/*      */ package com.waveset.adapter;
/*      */ 
/*      */ import com.sun.idm.logging.trace.Trace;
/*      */ import com.sun.idm.logging.trace.TraceManager;
/*      */ import com.waveset.adapter.iapi.IAPI;
/*      */ import com.waveset.adapter.iapi.IAPIException;
/*      */ import com.waveset.adapter.iapi.IAPIFactory;
/*      */ import com.waveset.adapter.iapi.IAPIProcess;
/*      */ import com.waveset.adapter.util.OracleLobUtil;
/*      */ import com.waveset.msgcat.ErrorMessage;
/*      */ import com.waveset.msgcat.Message;
/*      */ import com.waveset.msgcat.Severity;
/*      */ import com.waveset.object.AccountAttributeType;
/*      */ import com.waveset.object.Attribute;
/*      */ import com.waveset.object.GenericObject;
/*      */ import com.waveset.object.Resource;
/*      */ import com.waveset.object.ResourceInfo;
/*      */ import com.waveset.object.Syntax;
/*      */ import com.waveset.object.WSAttribute;
/*      */ import com.waveset.object.WSUser;
/*      */ import com.waveset.object.WavesetResult;
/*      */ import com.waveset.ui.web.resources.wizard.DBTableUtil;
/*      */ import com.waveset.util.Binary;
/*      */ import com.waveset.util.EncryptedData;
/*      */ import com.waveset.util.InternalError;
/*      */ import com.waveset.util.PooledConnection;
/*      */ import com.waveset.util.Util;
/*      */ import com.waveset.util.VariableExpander;
/*      */ import com.waveset.util.VariableResolver;
/*      */ import com.waveset.util.WavesetException;
/*      */ import java.io.ByteArrayInputStream;
/*      */ import java.io.IOException;
/*      */ import java.io.InputStream;
/*      */ import java.io.OutputStream;
/*      */ import java.io.PrintStream;
/*      */ import java.sql.Blob;
/*      */ import java.sql.Connection;
/*      */ import java.sql.DatabaseMetaData;
/*      */ import java.sql.PreparedStatement;
/*      */ import java.sql.ResultSet;
/*      */ import java.sql.ResultSetMetaData;
/*      */ import java.sql.SQLException;
/*      */ import java.util.ArrayList;
/*      */ import java.util.HashMap;
/*      */ import java.util.Iterator;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import java.util.Map.Entry;
/*      */ import java.util.NoSuchElementException;
/*      */ import java.util.Set;
/*      */ 
/*      */ public class DatabaseTableResourceAdapter extends JdbcResourceAdapter
/*      */   implements ActiveSync
/*      */ {
/*      */   public static final String code_id = "$Id: DatabaseTableResourceAdapter.java,v 1.52 2009/01/13 04:50:28 sb154400 Exp $";
/*      */   private static final String CLASS = "com.waveset.adapter.DatabaseTableResourceAdapter";
/*   98 */   private static final Trace TRACE = TraceManager.getTrace("com.waveset.adapter.DatabaseTableResourceAdapter");
/*      */   public static final String RA_TABLE = "Table";
/*      */   public static final String RA_KEY_COLUMN = "Key Column";
/*      */   public static final String RA_PASSWORD_COLUMN = "Password Column";
/*      */   public static final String RA_NO_PASSWORDS = "noPasswords";
/*      */   public static final String RA_ENABLE_EMPTY_STRING = "enableEmptyString";
/*      */   public static final String RA_NATIVE_TIMESTAMPS = "nativeTimestamps";
/*      */   public static final String DEFAULT_PORT = "1521";
/*      */   public static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
/*      */   public static final String DEFAULT_TEMPLATE = "jdbc:oracle:thin:@%h:%p:%d";
/*      */   ActiveSyncUtil _util;
/*      */   public static final String RA_STATIC_SEARCH_PREDICATE = "Static Search Predicate";
/*      */ 
/*      */   /** @deprecated */
/*      */   public static final String RA_PROCESS_NAME = "Process to run with changes";
/*      */   public static final String LAST_FETCHED_PREDICATE = "Last Fetched Predicate";
/*      */   public static final String LAST_FETCHED_CONJUNCTION = "Last Fetched Conjunction";
/*      */   public static final String ORDER_BY_CLAUSE = "Order By Clause";
/*      */   private static final String PROP_PROCESSED = "lastProcessed";
/*  157 */   static final String prototypeXml = "<Resource name='Database Table'           class='com.waveset.adapter.DatabaseTableResourceAdapter'           typeString='Database Table'   syncEnabled='Enabled'        syncSource='true'  facets='provision' >\n  <ResourceAttributes>\n    <ResourceAttribute name='Quoting' displayName='RAMessages:RESATTR_DBTABLE_QUOTING'\n      description='RAMessages:RESATTR_HELP_DBTABLE_QUOTING'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Host' displayName='RESATTR_HOST'\n      description='RESATTR_HELP_DBTABLE_HOST'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Port' displayName='RESATTR_PORT' value='1521'\n      description='RESATTR_HELP_DBTABLE_PORT'>\n    </ResourceAttribute>\n    <ResourceAttribute name='User' displayName='RESATTR_USER'\n      description='RESATTR_HELP_DBTABLE_USER'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Password' displayName='RESATTR_PASSWORD' type='encrypted'\n      description='RESATTR_HELP_DBTABLE_PASSWORD'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Database' displayName='RESATTR_DATABASE'\n       description='RESATTR_HELP_DBTABLE_DATABASE'\n>    </ResourceAttribute>\n    <ResourceAttribute name='Table' displayName='RESATTR_TABLE'\n       description='RESATTR_HELP_DBTABLE_TABLE'\n>    </ResourceAttribute>\n    <ResourceAttribute name='Key Column' displayName='RESATTR_KEY_COLUMN'\n       description='RESATTR_HELP_DBTABLE_KEY_COLUMN'\n>    </ResourceAttribute>\n    <ResourceAttribute name='Password Column' displayName='RESATTR_PASSWORD_COLUMN'\n       description='RESATTR_HELP_DBTABLE_PASSWORD_COLUMN'\n>    </ResourceAttribute>\n    <ResourceAttribute name='JDBC Driver' displayName='RESATTR_JDBC_DRIVER'\n      description='RESATTR_HELP_DBTABLE_DRIVER'      value='oracle.jdbc.driver.OracleDriver'>\n    </ResourceAttribute>\n    <ResourceAttribute name='JDBC URL Template' displayName='RESATTR_URL_TEMPLATE'\n      description='RESATTR_HELP_DBTABLE_URL_TEMPLATE'\n      value='jdbc:oracle:thin:@%h:%p:%d'>\n    </ResourceAttribute>\n    <ResourceAttribute name='idleTimeout' displayName='com.waveset.adapter.RAMessages:RESATTR_DBPOOL_IDLETIMEOUT' type='string' multi='false'\n       description='com.waveset.adapter.RAMessages:RESATTR_DBPOOL_IDLETIMEOUT_HELP'\n       value='600'>\n    </ResourceAttribute>\n    <ResourceAttribute name='enableEmptyString' displayName='com.waveset.adapter.RAMessages:RESATTR_DBTABLE_ENABLE_EMPTY_STRING' type='boolean'\n      description='com.waveset.adapter.RAMessages:RESATTR_DBTABLE_ENABLE_EMPTY_STRING_HELP'\n      value='false'>\n    </ResourceAttribute>\n     <ResourceAttribute name='noPasswords' displayName='com.waveset.adapter.RAMessages:RESATTR_NOPASSWORDS' type='" + Syntax.BOOLEAN.getName() + "'\n" + "        description='" + "com.waveset.adapter.RAMessages:DBTABLE_NOPASSWORDS_HELP" + "'/>\n" + "     <ResourceAttribute name='" + "rethrowAllSQLExceptions" + "' displayName='" + "com.waveset.adapter.RAMessages:RESATTR_RETHROW_ALL_SQLEXCEPTIONS" + "' type='" + Syntax.BOOLEAN.getName() + "'\n" + "        description='" + "com.waveset.adapter.RAMessages:RESATTR_HELP_RETHROW_ALL_SQLEXCEPTIONS" + "'/>\n" + "     <ResourceAttribute name='" + "nativeTimestamps" + "' displayName='" + "com.waveset.adapter.RAMessages:RESATTR_NATIVE_TIMESTAMPS" + "' type='" + Syntax.BOOLEAN.getName() + "'\n" + "        description='" + "com.waveset.adapter.RAMessages:DBTABLE_NATIVE_TIMESTAMPS_HELP" + "'/>\n" + "    <ResourceAttribute name='" + "Static Search Predicate" + "' displayName='" + "com.waveset.adapter.RAMessages:RESATTR_STATIC_SEARCH_PREDICATE" + "' type='string' multi='false'\n" + "      description='" + "com.waveset.adapter.RAMessages:DBTABLE_HELP_STATIC_SEARCH_PREDICATE" + "' facets='activesync' >\n" + "   </ResourceAttribute>\n" + "    <ResourceAttribute name='" + "Last Fetched Conjunction" + "' displayName='" + "com.waveset.adapter.RAMessages:RESATTR_LAST_FETCHED_CONJUNCTION" + "' type='string' multi='false' value='AND' \n" + "      description='" + "com.waveset.adapter.RAMessages:DBTABLE_LAST_FETCHED_CONJUNCTION_HELP" + "' facets='activesync' >\n" + "    </ResourceAttribute>\n" + "    <ResourceAttribute name='" + "Last Fetched Predicate" + "' displayName='" + "com.waveset.adapter.RAMessages:RESATTR_LAST_FETCHED_PREDICATE" + "' type='string' multi='false'\n" + "      description='" + "com.waveset.adapter.RAMessages:DBTABLE_LAST_FETCHED_PREDICATE_HELP" + "' facets='activesync' >\n" + "    </ResourceAttribute>\n" + "    <ResourceAttribute name='" + "Order By Clause" + "' displayName='" + "com.waveset.adapter.RAMessages:DBTABLE_ORDER_BY" + "' type='string' multi='false'\n" + "      description='" + "com.waveset.adapter.RAMessages:DBTABLE_ORDER_BY_HELP" + "' facets='activesync' >\n" + "    </ResourceAttribute>\n" + "    <ResourceAttribute name='ignoreEventFailures' displayName='com.waveset.adapter.RAMessages:RESATTR_IGN_ACT_SYNC_FAILURES' type='boolean' multi='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_IGN_ACT_SYNC_FAILURES_HELP' facets='activesync' >\n    </ResourceAttribute>\n" + "    <ResourceAttribute name='processRule' displayName='com.waveset.adapter.RAMessages:RESATTR_PROCESS_RULE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_PROCESS_RULE_HELP' facets='activesync'>\n    </ResourceAttribute>\n    <ResourceAttribute name='correlationRule' displayName='com.waveset.adapter.RAMessages:RESATTR_CORRELATION_RULE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_CORRELATION_RULE_HELP' facets='activesync' value='CORRELATION_RULE_NONE'>\n    </ResourceAttribute>\n    <ResourceAttribute name='confirmationRule' displayName='com.waveset.adapter.RAMessages:RESATTR_CONFIRMATION_RULE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_CONFIRMATION_RULE_HELP' facets='activesync' value='CONFIRMATION_RULE_NONE'>\n    </ResourceAttribute>\n    <ResourceAttribute name='deleteRule' displayName='com.waveset.adapter.RAMessages:RESATTR_DELETE_RULE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_DELETE_RULE_HELP' facets='activesync'>\n    </ResourceAttribute>\n    <ResourceAttribute name='createUnmatched' displayName='com.waveset.adapter.RAMessages:RESATTR_CREATE_UNMATCHED' type='string' multi='false' value='true'\n      description='com.waveset.adapter.RAMessages:RESATTR_CREATE_UNMATCHED_HELP' facets='activesync'>\n    </ResourceAttribute>\n    <ResourceAttribute name='resolveProcessRule' displayName='com.waveset.adapter.RAMessages:RESATTR_RESOLVE_PROCESS_RULE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_RESOLVE_PROCESS_RULE_HELP' facets='activesync'>\n    </ResourceAttribute>\n    <ResourceAttribute name='populateGlobal' displayName='com.waveset.adapter.RAMessages:RESATTR_POPULATE_GLOBAL' type='string' multi='false' value='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_POPULATE_GLOBAL_HELP' facets='activesync'>\n    </ResourceAttribute>\n" + "    <ResourceAttribute name='Proxy Administrator' displayName='com.waveset.adapter.RAMessages:RESATTR_PROXY_ADMINISTRATOR' type='string' multi='false' value='Configurator'\n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_30'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Input Form' displayName='com.waveset.adapter.RAMessages:RESATTR_FORM' type='string' multi='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_26'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Pre-Poll Workflow' displayName='com.waveset.adapter.RAMessages:RESATTR_PREPOLL_WORKFLOW' type='string' multi='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_PREPOLL_WORKFLOW_HELP'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Post-Poll Workflow' displayName='com.waveset.adapter.RAMessages:RESATTR_POSTPOLL_WORKFLOW' type='string' multi='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_POSTPOLL_WORKFLOW_HELP'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Maximum Archives' displayName='com.waveset.adapter.RAMessages:RESATTR_MAX_ARCHIVES' type='string' multi='false'  value='3'\n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_MAX_ARCHIVES'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Maximum Age Length' displayName='com.waveset.adapter.RAMessages:RESATTR_MAX_LOG_AGE' type='string' multi='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_MAX_LOG_AGE'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Maximum Age Unit' displayName='com.waveset.adapter.RAMessages:RESATTR_MAX_LOG_AGE_UNIT' type='string' multi='false'\n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_MAX_LOG_AGE_UNIT'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Log Level' displayName='com.waveset.adapter.RAMessages:RESATTR_LOG_LEVEL' type='string' multi='false' value='2'\n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_27'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Log File Path' displayName='com.waveset.adapter.RAMessages:RESATTR_LOG_PATH' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_28'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Maximum Log File Size' displayName='com.waveset.adapter.RAMessages:RESATTR_LOG_SIZE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_29'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Scheduling Interval' displayName='com.waveset.adapter.RAMessages:RESATTR_SCHEDULE_INTERVAL' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_51'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Poll Every' displayName='com.waveset.adapter.RAMessages:RESATTR_SCHEDULE_INTERVAL_COUNT' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_52'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Polling Start Time' displayName='com.waveset.adapter.RAMessages:RESATTR_SCHEDULE_START_TIME' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_56'>\n    </ResourceAttribute>\n    <ResourceAttribute name='Polling Start Date' displayName='com.waveset.adapter.RAMessages:RESATTR_SCHEDULE_START_DATE' type='string' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_HELP_54'>\n    </ResourceAttribute>\n    <ResourceAttribute name='assignSourceOnCreate' displayName='com.waveset.adapter.RAMessages:RESATTR_ASSIGN_SOURCE_ON_CREATE' type='boolean' multi='false' \n      description='com.waveset.adapter.RAMessages:RESATTR_ASSIGN_SOURCE_ON_CREATE_HELP' facets='activesync' value='true'>\n    </ResourceAttribute>\n" + "  </ResourceAttributes>\n" + "  <Template>\n" + "    <AttrDef name='accountId' type='string' />\n" + "  </Template>\n" + "<AccountAttributeTypes>\n" + "</AccountAttributeTypes>\n" + "  <LoginConfigEntry name='" + "com.waveset.security.authn.WSResourceLoginModule" + "' type='" + "Database Table" + "' displayName='" + "com.waveset.adapter.RAMessages:RES_LOGIN_MOD_DBTABLE" + "'>\n" + "    <AuthnProperties>\n" + "      <AuthnProperty name='" + "db_user" + "' displayName='" + "com.waveset.adapter.RAMessages:UI_USERID_LABEL" + "' formFieldType='text' isId='true' />\n" + "      <AuthnProperty name='" + "db_password" + "' displayName='" + "com.waveset.adapter.RAMessages:UI_PWD_LABEL" + "' formFieldType='password'/>\n" + "    </AuthnProperties>\n" + "    <SupportedApplications>\n" + "      <SupportedApplication name='" + "Administrator Interface" + "'/>\n" + "      <SupportedApplication name='" + "User Interface" + "'/>\n" + "    </SupportedApplications>\n" + "  </LoginConfigEntry>\n" + "  <ResourceUserForm>\n" + "    <ObjectRef id='" + "#ID#" + "Database Table User Form' type='" + "UserForm" + "'/>\n" + "  </ResourceUserForm>\n" + "</Resource>\n";
/*      */   private HashMap _nativeTimestampsMap;
/*      */ 
/*      */   public DatabaseTableResourceAdapter()
/*      */   {
/*  134 */     this._util = null;
/*      */ 
/*  281 */     this._nativeTimestampsMap = null;
/*      */   }
/*      */ 
/*      */   public static Resource staticCreatePrototypeResource()
/*      */     throws WavesetException
/*      */   {
/*  288 */     Resource res = new Resource(prototypeXml);
/*      */ 
/*  291 */     res.setResourceAttributeVal("correlationRule", null);
/*      */ 
/*  294 */     res.setResourceAttributeVal("confirmationRule", null);
/*      */ 
/*  296 */     return res;
/*      */   }
/*      */ 
/*      */   public Resource createPrototypeResource() throws WavesetException {
/*  300 */     return staticCreatePrototypeResource();
/*      */   }
/*      */ 
/*      */   public void startConnection()
/*      */     throws WavesetException
/*      */   {
/*      */   }
/*      */ 
/*      */   public void stopConnection()
/*      */     throws WavesetException
/*      */   {
/*      */   }
/*      */ 
/*      */   private String getPassword(WSUser user)
/*      */   {
/*  324 */     if (user == null) return null;
/*      */ 
/*  326 */     String password = null;
/*  327 */     ResourceInfo resInfo = user.getResourceInfo(this._resource);
/*  328 */     if (resInfo != null) {
/*  329 */       EncryptedData ePassword = resInfo.getPassword();
/*  330 */       if (ePassword != null) {
/*  331 */         password = ePassword.decryptToString();
/*      */       }
/*      */     }
/*      */ 
/*  335 */     return password;
/*      */   }
/*      */ 
/*      */   public WavesetResult checkCreateAccount(WSUser user)
/*      */     throws WavesetException
/*      */   {
/*  345 */     return new WavesetResult();
/*      */   }
/*      */ 
/*      */   protected void realCreate(WSUser user, WavesetResult result)
/*      */     throws WavesetException
/*      */   {
/*  353 */     String METHOD = "realCreate";
/*  354 */     TRACE.entry2("realCreate", "user=" + user);
/*  355 */     String id = getIdentity(user);
/*  356 */     Object rowKeyObj = getRequiredResAttrVal("Key Column");
/*  357 */     String rowPass = getOptionalStringResAttrVal("Password Column");
/*      */ 
/*  359 */     if (id == null) {
/*  360 */       String msg = "Could not get identity.";
/*  361 */       WavesetException we = new WavesetException("Could not get identity.", Severity.ERROR);
/*  362 */       TRACE.throwing2("realCreate", we);
/*  363 */       throw we;
/*      */     }
/*      */ 
/*  366 */     ArrayList params = new ArrayList();
/*      */ 
/*  370 */     String keyColumn = rowKeyObj.toString();
/*  371 */     StringBuffer columns = new StringBuffer();
/*  372 */     addQuotedName(columns, keyColumn);
/*  373 */     StringBuffer values = new StringBuffer();
/*  374 */     values.append("?");
/*  375 */     params.add(new Param(keyColumn, getResourceDefinedType(keyColumn), id));
/*      */ 
/*  378 */     String userPass = getPassword(user);
/*  379 */     if ((userPass != null) && (rowPass != null)) {
/*  380 */       columns.append(", ");
/*  381 */       addQuotedName(columns, rowPass);
/*  382 */       values.append(", ?");
/*  383 */       params.add(new Param(rowPass, Syntax.STRING, userPass));
/*      */     }
/*      */ 
/*  386 */     AccountAttributeType[] attrs = this._resource.getAccountAttributeTypes();
/*  387 */     for (int i = 0; i < attrs.length; i++) {
/*  388 */       String attrName = attrs[i].getName();
/*  389 */       String mapName = attrs[i].getMapName();
/*  390 */       Syntax syntax = attrs[i].getSyntax();
/*  391 */       WSAttribute attr = user.getWSAttribute(attrName);
/*  392 */       if (attr == null)
/*      */       {
/*      */         continue;
/*      */       }
/*      */ 
/*  397 */       if ((Util.equalStrings(mapName, keyColumn)) || (Util.equalStrings(mapName, rowPass)))
/*      */       {
/*      */         continue;
/*      */       }
/*  401 */       Object value = attr.getValue();
/*      */ 
/*  403 */       Syntax syn = Syntax.findSyntax(attr.getType());
/*  404 */       if (Syntax.ENCRYPTED.equals(syn)) {
/*  405 */         if ((value != null) && 
/*  406 */           ((value instanceof EncryptedData))) {
/*  407 */           value = ((EncryptedData)value).decryptToString();
/*      */         }
/*      */ 
/*  410 */         syntax = Syntax.STRING;
/*      */       }
/*      */ 
/*  414 */       columns.append(", ");
/*  415 */       addQuotedName(columns, mapName);
/*      */ 
/*  417 */       values.append(", ?");
/*  418 */       params.add(new Param(mapName, syntax, value));
/*      */     }
/*      */ 
/*  421 */     Connection con = null;
/*      */     try {
/*  423 */       con = getConnection();
/*      */ 
/*  425 */       HashMap columnsNullable = getColumnsNullable(con);
/*      */ 
/*  428 */       Iterator it = columnsNullable.entrySet().iterator();
/*  429 */       while (it.hasNext()) {
/*  430 */         Map.Entry col = (Map.Entry)it.next();
/*  431 */         String nullable = (String)col.getValue();
/*  432 */         String column = (String)col.getKey();
/*      */ 
/*  435 */         if ((nullable.equalsIgnoreCase("NO")) && (columns.indexOf(column) == -1)) {
/*  436 */           columns.append(", ");
/*  437 */           addQuotedName(columns, column);
/*  438 */           values.append(", ?");
/*      */ 
/*  440 */           Syntax syntax = Syntax.STRING;
/*  441 */           for (int i = 0; i < attrs.length; i++) {
/*  442 */             if (column == attrs[i].getMapName()) {
/*  443 */               syntax = attrs[i].getSyntax();
/*  444 */               break;
/*      */             }
/*      */           }
/*  447 */           params.add(new Param(column, syntax, (Object)null));
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/*  452 */       StringBuffer b = new StringBuffer(64);
/*  453 */       b.append("insert into ");
/*  454 */       b.append(getRequiredResAttrVal("Table"));
/*  455 */       b.append(" ( ");
/*  456 */       b.append(columns.toString());
/*  457 */       b.append(" ) values ( ");
/*  458 */       b.append(values.toString());
/*  459 */       b.append(" )");
/*      */ 
/*  461 */       if (TRACE.level3("realCreate")) {
/*  462 */         StringBuffer msg = new StringBuffer(96);
/*  463 */         msg.append("Executing query: ");
/*  464 */         msg.append(b.toString());
/*  465 */         TRACE.info(0L, "realCreate", msg.toString());
/*      */       }
/*      */ 
/*  468 */       Param[] paramArray = (Param[])(Param[])params.toArray(new Param[0]);
/*  469 */       execSqlWithParams(id, con, b.toString(), paramArray, columnsNullable);
/*      */ 
/*  474 */       ResourceInfo resInfo = user.getResourceInfo(this._resource);
/*  475 */       if (resInfo != null) {
/*  476 */         resInfo.setAccountCreated(true);
/*      */ 
/*  478 */         resInfo.setPassword((EncryptedData)null);
/*      */       }
/*      */     }
/*      */     finally {
/*  482 */       closeConnection(con);
/*      */     }
/*      */ 
/*  485 */     if (TRACE.level3("realCreate")) {
/*  486 */       StringBuffer sb = new StringBuffer("DatabaseTableResourceAdapter: Added user '");
/*      */ 
/*  488 */       sb.append(id);
/*  489 */       sb.append("'");
/*  490 */       TRACE.info(0L, "realCreate", sb.toString());
/*      */     }
/*      */ 
/*  493 */     TRACE.exit2("realCreate", result);
/*      */   }
/*      */ 
/*      */   public WavesetResult checkUpdateAccount(WSUser user)
/*      */     throws WavesetException
/*      */   {
/*  503 */     return new WavesetResult();
/*      */   }
/*      */ 
/*      */   protected void realUpdate(WSUser user, WavesetResult result)
/*      */     throws WavesetException
/*      */   {
/*  512 */     String METHOD = "realUpdate";
/*  513 */     TRACE.entry2("realUpdate", "user=" + user);
/*  514 */     String id = getIdentity(user);
/*      */ 
/*  516 */     ArrayList params = new ArrayList();
/*  517 */     StringBuffer updates = new StringBuffer();
/*  518 */     AccountAttributeType[] attrs = this._resource.getAccountAttributeTypes();
/*  519 */     for (int i = 0; i < attrs.length; i++) {
/*  520 */       WSAttribute wsAttr = user.getWSAttribute(attrs[i].getName());
/*  521 */       if (wsAttr == null)
/*      */       {
/*      */         continue;
/*      */       }
/*      */ 
/*  528 */       Object value = wsAttr.getValue();
/*      */ 
/*  530 */       if (updates.length() > 0) {
/*  531 */         updates.append(", ");
/*      */       }
/*  533 */       String columnName = attrs[i].getMapName();
/*  534 */       addQuotedName(updates, columnName);
/*  535 */       updates.append(" = ?");
/*  536 */       params.add(new Param(columnName, attrs[i].getSyntax(), value));
/*      */     }
/*      */ 
/*  543 */     String accountId = user.getAttribute("ws_newAccountId");
/*  544 */     TRACE.info3("realUpdate", "new accountid = " + accountId);
/*      */ 
/*  546 */     if (null != accountId) {
/*  547 */       if (updates.length() > 0) {
/*  548 */         updates.append(", ");
/*      */       }
/*  550 */       String keyColumn = getRequiredResAttrVal("Key Column").toString();
/*  551 */       addQuotedName(updates, keyColumn);
/*  552 */       updates.append(" = ?");
/*  553 */       params.add(new Param(keyColumn, getResourceDefinedType(keyColumn), accountId));
/*      */     }
/*      */ 
/*  559 */     String userPass = getPassword(user);
/*  560 */     String rowPass = getOptionalStringResAttrVal("Password Column");
/*  561 */     if ((userPass != null) && (rowPass != null)) {
/*  562 */       if (updates.length() > 0) updates.append(", ");
/*  563 */       addQuotedName(updates, rowPass);
/*  564 */       updates.append(" = ?");
/*  565 */       params.add(new Param(rowPass, Syntax.STRING, userPass));
/*      */     }
/*      */ 
/*  569 */     if (updates.length() > 0)
/*      */     {
/*  574 */       StringBuffer b = new StringBuffer();
/*  575 */       b.append("update ");
/*  576 */       b.append(getRequiredResAttrVal("Table"));
/*  577 */       b.append(" set ");
/*  578 */       b.append(updates.toString());
/*  579 */       b.append(" where ");
/*  580 */       String keyColumn = getRequiredResAttrVal("Key Column").toString();
/*  581 */       addQuotedName(b, keyColumn);
/*  582 */       b.append(" = ?");
/*  583 */       params.add(new Param(keyColumn, getResourceDefinedType(keyColumn), id));
/*      */ 
/*  585 */       Connection con = getConnection();
/*      */       try {
/*  587 */         if (TRACE.level3("realUpdate")) {
/*  588 */           StringBuffer msg = new StringBuffer(96);
/*  589 */           msg.append("Executing query: ");
/*  590 */           msg.append(b.toString());
/*  591 */           TRACE.info(0L, "realUpdate", msg.toString());
/*      */         }
/*      */ 
/*  595 */         HashMap columnsNullable = getColumnsNullable(con);
/*      */ 
/*  597 */         Param[] paramArray = (Param[])(Param[])params.toArray(new Param[0]);
/*  598 */         execSqlWithParams(id, con, b.toString(), paramArray, columnsNullable);
/*      */ 
/*  603 */         ResourceInfo resInfo = user.getResourceInfo(this._resource);
/*  604 */         if (resInfo != null) {
/*  605 */           resInfo.setAccountCreated(true);
/*      */ 
/*  607 */           resInfo.setPassword((EncryptedData)null);
/*  608 */           if (null != accountId)
/*  609 */             resInfo.setAccountId(accountId);
/*      */         }
/*      */       }
/*      */       finally
/*      */       {
/*  614 */         closeConnection(con);
/*      */       }
/*      */     }
/*      */ 
/*  618 */     if (TRACE.level3("realUpdate")) {
/*  619 */       StringBuffer msg = new StringBuffer("DatabaseTableResourceAdapter: Updated user '");
/*      */ 
/*  621 */       msg.append(id);
/*  622 */       msg.append("'");
/*  623 */       TRACE.info(0L, "realUpdate", msg.toString());
/*      */     }
/*      */ 
/*  626 */     TRACE.exit2("realUpdate", result);
/*      */   }
/*      */ 
/*      */   public WavesetResult checkDeleteAccount(WSUser user)
/*      */     throws WavesetException
/*      */   {
/*  636 */     return new WavesetResult();
/*      */   }
/*      */ 
/*      */   protected void realDelete(WSUser user, WavesetResult result)
/*      */     throws WavesetException
/*      */   {
/*  646 */     String METHOD = "realDelete";
/*  647 */     TRACE.entry2("realDelete", "user=" + user);
/*  648 */     String id = getIdentity(user);
/*      */ 
/*  651 */     StringBuffer b = new StringBuffer();
/*  652 */     b.append("delete from ");
/*  653 */     b.append(getRequiredResAttrVal("Table"));
/*  654 */     b.append(" where ");
/*  655 */     String keyColumn = getRequiredResAttrVal("Key Column").toString();
/*  656 */     addQuotedName(b, keyColumn);
/*  657 */     b.append(" = ?");
/*      */ 
/*  659 */     Param[] paramArray = new Param[1];
/*  660 */     paramArray[0] = new Param(keyColumn, getResourceDefinedType(keyColumn), id);
/*      */ 
/*  662 */     Connection con = getConnection();
/*      */     try
/*      */     {
/*  665 */       if (TRACE.level3("realDelete")) {
/*  666 */         StringBuffer msg = new StringBuffer(96);
/*  667 */         msg.append("Executing query: ");
/*  668 */         msg.append(b.toString());
/*  669 */         TRACE.info(0L, "realDelete", msg.toString());
/*      */       }
/*      */ 
/*  672 */       execSqlWithParams(id, con, b.toString(), paramArray);
/*      */     }
/*      */     finally {
/*  675 */       closeConnection(con);
/*      */     }
/*      */ 
/*  678 */     if (TRACE.level3("realDelete")) {
/*  679 */       StringBuffer msg = new StringBuffer("Deleted user '");
/*  680 */       msg.append(id);
/*  681 */       msg.append("'");
/*  682 */       TRACE.info(0L, "realDelete", msg.toString());
/*      */     }
/*      */ 
/*  685 */     TRACE.exit2("realDelete", result);
/*      */   }
/*      */ 
/*      */   public WSUser getUser(WSUser idUser)
/*      */     throws WavesetException
/*      */   {
/*  698 */     String METHOD = "getUser(WSUser)";
/*  699 */     TRACE.entry1("getUser(WSUser)", "idUser=" + idUser);
/*  700 */     WSUser user = null;
/*      */ 
/*  702 */     Connection con = getConnection();
/*      */     try {
/*  704 */       user = getUser(con, getIdentity(idUser));
/*      */     }
/*      */     finally {
/*  707 */       closeConnection(con);
/*      */     }
/*      */ 
/*  710 */     TRACE.exit1("getUser(WSUser)", user);
/*  711 */     return user;
/*      */   }
/*      */ 
/*      */   private WSUser getUser(Connection con, String id)
/*      */     throws WavesetException
/*      */   {
/*  719 */     String METHOD = "getUser(Connection, String)";
/*  720 */     TRACE.entry2("getUser(Connection, String)");
/*  721 */     if (TRACE.level2("getUser(Connection, String)")) {
/*  722 */       StringBuffer sb = new StringBuffer(48);
/*  723 */       sb.append("con=");
/*  724 */       sb.append(getConnectionString(con));
/*  725 */       sb.append(",id=");
/*  726 */       sb.append(id);
/*  727 */       TRACE.info2("getUser(Connection, String)", sb.toString());
/*      */     }
/*      */ 
/*  730 */     WSUser user = null;
/*      */ 
/*  732 */     PreparedStatement st = null;
/*  733 */     ResultSet res = null;
/*      */ 
/*  735 */     StringBuffer b = new StringBuffer();
/*      */ 
/*  747 */     b.append("select ");
/*      */ 
/*  749 */     Resource resource0 = getResource();
/*  750 */     AccountAttributeType[] attrs0 = resource0.getAccountAttributeTypes();
/*  751 */     for (int i = 0; i < attrs0.length; i++) {
/*  752 */       addQuotedName(b, attrs0[i].getMapName());
/*  753 */       if (i < attrs0.length - 1) {
/*  754 */         b.append(",");
/*      */       }
/*      */     }
/*      */ 
/*  758 */     b.append(" from ");
/*      */ 
/*  760 */     b.append(getRequiredResAttrVal("Table"));
/*  761 */     b.append(" where ");
/*  762 */     String keyColumn = getRequiredResAttrVal("Key Column").toString();
/*  763 */     addQuotedName(b, keyColumn);
/*  764 */     b.append(" = ?");
/*      */     try
/*      */     {
/*  767 */       if (TRACE.level3("getUser(Connection, String)")) {
/*  768 */         StringBuffer msg = new StringBuffer("Executing query: ");
/*  769 */         msg.append(b.toString());
/*  770 */         msg.append(" (for id = " + id + ")");
/*  771 */         TRACE.info(0L, "getUser(Connection, String)", msg.toString());
/*      */       }
/*  773 */       st = con.prepareStatement(b.toString());
/*  774 */       Syntax syntaxKeyColumn = getResourceDefinedType(keyColumn);
/*  775 */       if (Syntax.INT.equals(syntaxKeyColumn)) {
/*  776 */         int integerValue = 0;
/*      */         try {
/*  778 */           integerValue = Integer.parseInt(id);
/*      */         } catch (NumberFormatException e) {
/*  780 */           String msg = "Number Format Exception : " + id + " is not a number.";
/*  781 */           WavesetException we = new WavesetException(msg, Severity.ERROR);
/*  782 */           TRACE.throwing2("getUser(Connection, String)", we);
/*  783 */           throw we;
/*      */         }
/*  785 */         st.setInt(1, integerValue);
/*      */       } else {
/*  787 */         st.setString(1, id);
/*      */       }
/*  789 */       res = st.executeQuery();
/*  790 */       if (res.next()) {
/*  791 */         user = new WSUser();
/*  792 */         user.setName(id);
/*      */ 
/*  796 */         ResourceInfo info = new ResourceInfo();
/*  797 */         info.setResource(this._resource);
/*  798 */         info.setAccountID(id);
/*  799 */         user.addResourceInfo(info);
/*      */ 
/*  801 */         Resource resource = getResource();
/*  802 */         AccountAttributeType[] attrs = resource.getAccountAttributeTypes();
/*  803 */         for (int i = 0; i < attrs.length; i++) {
/*  804 */           Syntax syntax = attrs[i].getSyntax();
/*  805 */           Object val = null;
/*  806 */           if (Syntax.BINARY.equals(syntax))
/*      */           {
/*  808 */             byte[] bytes = null;
/*  809 */             Object binaryObj = res.getObject(attrs[i].getMapName());
/*  810 */             if (binaryObj != null) {
/*  811 */               bytes = getBytesFromObject(attrs[i].getMapName(), binaryObj);
/*      */             }
/*      */ 
/*  814 */             if (bytes != null)
/*  815 */              //val = new Binary(bytes);
						val = bytes;
/*      */           }
/*      */           else {
/*  818 */             String attrMapName = attrs[i].getMapName();
/*  819 */             val = res.getString(attrMapName);
/*  820 */             if (checkNativeTimestamps(resource, res))
/*      */             {
/*  822 */               String mapVal = (String)this._nativeTimestampsMap.get(attrMapName);
/*  823 */               if ((mapVal != null) && (mapVal.equals("TIMESTAMP")))
/*      */               {
/*  825 */                 Object ts = res.getTimestamp(attrMapName);
/*  826 */                 val = ts != null ? ts.toString() : null;
/*      */               }
/*      */             }
/*      */ 
/*  830 */             val = Syntax.convertValue(syntax, val);
/*      */           }
/*  832 */           if (val != null) {
/*  833 */             WSAttribute wsAttr = new WSAttribute(attrs[i].getName(), val, syntax);
/*  834 */             user.setWSAttribute(wsAttr);
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*      */     catch (SQLException e) {
/*  840 */       TRACE.throwing1("getUser(Connection, String)", e);
/*  841 */       throw new InternalError(e);
/*      */     }
/*      */     finally {
/*  844 */       closeResult(res);
/*  845 */       closeStatement(st);
/*      */     }
/*      */ 
/*  849 */     if ((user != null) && (user.getAccountId() == null)) {
/*  850 */       user.setAccountId(user.getName());
/*      */     }
/*  852 */     TRACE.exit(0L, "getUser(Connection, String)", user);
/*      */ 
/*  854 */     return user;
/*      */   }
/*      */ 
/*      */   protected byte[] getBytesFromObject(String columnName, Object binaryObj)
/*      */     throws SQLException, InternalError, WavesetException
/*      */   {
/*  868 */     byte[] bytes = null;
/*  869 */     if (binaryObj != null)
/*      */     {
/*  871 */       if ((binaryObj instanceof byte[])) {
/*  872 */         bytes = (byte[])(byte[])binaryObj;
/*      */       }
/*      */       else
/*      */       {
/*  876 */         InputStream in = null;
/*  877 */         if (OracleLobUtil.isOracleBlob(binaryObj)) {
/*  878 */           in = OracleLobUtil.getBlobBinaryStream(binaryObj);
/*      */         }
/*  880 */         else if (OracleLobUtil.isOracleClob(binaryObj)) {
/*  881 */           in = OracleLobUtil.getClobBinaryStream(binaryObj);
/*      */         }
/*  883 */         else if ((binaryObj instanceof Blob)) {
/*  884 */           in = ((Blob)binaryObj).getBinaryStream();
/*      */         }
/*      */         else {
/*  887 */           String className = binaryObj.getClass().getName();
/*  888 */           throw new WavesetException("Unknown conversion for binary column " + columnName + " from instance of " + className + " to byte[]", Severity.ERROR);
/*      */         }
/*      */ 
/*  891 */         bytes = Util.readInputStreamBytes(in, true);
/*      */       }
/*      */     }
/*  894 */     return bytes;
/*      */   }
/*      */ 
/*      */   protected void setBinaryParam(PreparedStatement statement, int statementIndex, byte[] value)
/*      */     throws SQLException
/*      */   {
/*  907 */     statement.setBytes(statementIndex, value);
/*      */   }
/*      */ 
/*      */   public WavesetResult authenticate(HashMap loginInfo)
/*      */     throws WavesetException
/*      */   {
/*  921 */     String METHOD = "authenticate";
/*  922 */     TRACE.entry1("authenticate");
/*  923 */     if (TRACE.level1("authenticate")) {
/*  924 */       StringBuffer sb = new StringBuffer(32);
/*  925 */       sb.append("loginInfo=");
/*  926 */       sb.append(loginInfo);
/*  927 */       TRACE.entry1("authenticate", sb.toString());
/*      */     }
/*      */ 
/*  930 */     WavesetResult result = new WavesetResult();
/*      */ 
/*  932 */     String accountId = (String)loginInfo.get("db_user");
/*  933 */     EncryptedData password = (EncryptedData)loginInfo.get("db_password");
/*      */ 
/*  936 */     String table = (String)getRequiredResAttrVal("Table");
/*  937 */     String passCol = (String)getRequiredResAttrVal("Password Column");
/*      */ 
/*  939 */     if ((accountId == null) || (password == null)) {
/*  940 */       WavesetException we = new WavesetException("Missing required login info.", Severity.ERROR);
/*      */ 
/*  942 */       TRACE.throwing1("authenticate", we);
/*  943 */       throw we;
/*      */     }
/*      */ 
/*  946 */     PreparedStatement st = null;
/*  947 */     ResultSet res = null;
/*      */ 
/*  949 */     StringBuffer b = new StringBuffer();
/*  950 */     b.append("select ");
/*  951 */     addQuotedName(b, passCol);
/*  952 */     b.append(" from ");
/*  953 */     b.append(table);
/*  954 */     b.append(" where ");
/*  955 */     String keyColumn = getRequiredResAttrVal("Key Column").toString();
/*  956 */     addQuotedName(b, keyColumn);
/*  957 */     b.append(" = ");
/*  958 */     addAttributeValue(b, accountId, keyColumn);
/*      */ 
/*  960 */     Connection con = getConnection();
/*      */     try {
/*  962 */       if (TRACE.level3("authenticate")) {
/*  963 */         StringBuffer msg = new StringBuffer(96);
/*  964 */         msg.append("Executing query: ");
/*  965 */         msg.append(b.toString());
/*  966 */         TRACE.info(0L, "authenticate", msg.toString());
/*      */       }
/*      */ 
/*  969 */       st = con.prepareStatement(b.toString());
/*  970 */       res = st.executeQuery();
/*  971 */       if (res.next()) {
/*  972 */         String official = res.getString(passCol);
/*      */ 
/*  974 */         if (official == null) {
/*  975 */           WavesetException we = new WavesetException("com.waveset.adapter.RAMessages:LG_INVALID_CREDENTIALS", Severity.ERROR);
/*      */ 
/*  977 */           TRACE.throwing1("authenticate", we);
/*  978 */           throw we;
/*      */         }
/*      */ 
/*  981 */         official = official.trim();
/*  982 */         if (!official.equals(password.decryptToString())) {
/*  983 */           WavesetException we = new WavesetException("com.waveset.adapter.RAMessages:LG_INVALID_CREDENTIALS", Severity.ERROR);
/*      */ 
/*  985 */           TRACE.throwing1("authenticate", we);
/*  986 */           throw we;
/*      */         }
/*      */ 
/*  990 */         result.addResult("AUTHENTICATED_IDENTITY", accountId);
/*      */       }
/*      */       else {
/*  993 */         WavesetException we = new WavesetException("com.waveset.adapter.RAMessages:LG_INVALID_CREDENTIALS", Severity.ERROR);
/*      */ 
/*  995 */         TRACE.throwing1("authenticate", we);
/*  996 */         throw we;
/*      */       }
/*      */     }
/*      */     catch (SQLException e) {
/* 1000 */       TRACE.throwing1("authenticate", e);
/* 1001 */       throw new InternalError(e);
/*      */     }
/*      */     finally {
/* 1004 */       closeResult(res);
/* 1005 */       closeStatement(st);
/*      */     }
/*      */ 
/* 1008 */     TRACE.exit1("authenticate", result);
/*      */ 
/* 1010 */     return result;
/*      */   }
/*      */ 
/*      */   public WavesetResult testConfiguration() throws WavesetException {
/* 1014 */     String METHOD = "testConfiguration";
/* 1015 */     TRACE.entry1("testConfiguration");
/* 1016 */     WavesetResult result = new WavesetResult();
/*      */ 
/* 1021 */     Connection con = null;
/*      */     try {
/* 1023 */       ResourceAdapterBase.getRequiredResAttrVal(this._resource, "Key Column");
/*      */ 
/* 1025 */       con = getConnection();
/* 1026 */       DBTableUtil.checkAccess(con, (String)getRequiredResAttrVal("Table"));
/*      */     }
/*      */     catch (Throwable t) {
/* 1029 */       TRACE.info1("testConfiguration", t.getMessage());
/* 1030 */       result.addException(t);
/*      */     }
/*      */     finally {
/* 1033 */       if (con != null) {
/* 1034 */         closeConnection(con);
/*      */       }
/*      */     }
/*      */ 
/* 1038 */     if (getResource().isActiveSync())
/*      */     {
/* 1042 */       WavesetResult activeSyncResult = activeSyncTestConfiguration();
/* 1043 */       result.assimilate(activeSyncResult);
/*      */     }
/*      */ 
/* 1047 */     TRACE.exit1("testConfiguration", result);
/* 1048 */     return result;
/*      */   }
/*      */ 
/*      */   protected void execSqlWithParams(String id, Connection con, String stmt, Param[] params)
/*      */     throws WavesetException
/*      */   {
/* 1073 */     HashMap columnsNullable = getColumnsNullable(con);
/* 1074 */     execSqlWithParams(id, con, stmt, params, columnsNullable);
/*      */   }
/*      */ 
/*      */   protected void execSqlWithParams(String id, Connection con, String stmt, Param[] params, HashMap columnsNullable)
/*      */     throws WavesetException
/*      */   {
/* 1087 */     execSqlWithParams(id, con, stmt, params, columnsNullable, 0);
/*      */   }
/*      */ 
/*      */   protected void execSqlWithParams(String id, Connection con, String stmt, Param[] params, HashMap columnsNullable, int ignoreError)
/*      */     throws WavesetException
/*      */   {
/* 1103 */     if (OracleLobUtil.usingOracleJDBC(con)) {
/* 1104 */       execOracleSqlWithParams(id, con, stmt, params, columnsNullable, ignoreError);
/*      */     }
/*      */     else
/* 1107 */       execStandardSqlWithParams(id, con, stmt, params, columnsNullable, ignoreError);
/*      */   }
/*      */ 
/*      */   protected void execStandardSqlWithParams(String id, Connection con, String stmt, Param[] params, HashMap columnsNullable, int ignoreError)
/*      */     throws WavesetException
/*      */   {
/* 1124 */     String METHOD = "execStandardSqlWithParams";
/* 1125 */     TRACE.entry2("execStandardSqlWithParams");
/* 1126 */     if (TRACE.level2("execStandardSqlWithParams")) {
/* 1127 */       StringBuffer sb = new StringBuffer(64);
/* 1128 */       sb.append("\nconnection=");
/* 1129 */       sb.append(getConnectionString(con));
/* 1130 */       sb.append("\nstatement=");
/* 1131 */       sb.append(stmt);
/* 1132 */       for (int i = 0; (params != null) && (i < params.length); i++) {
/* 1133 */         sb.append("\narg[" + (i + 1) + "]=");
/* 1134 */         sb.append(params[i].value);
/* 1135 */         sb.append(" [syntax=");
/* 1136 */         sb.append(params[i].syntax);
/* 1137 */         sb.append("]");
/*      */       }
/* 1139 */       sb.append("\nignoreError=");
/* 1140 */       sb.append(ignoreError);
/* 1141 */       TRACE.info2("execStandardSqlWithParams", sb.toString());
/*      */     }
/*      */ 
/* 1144 */     PreparedStatement s = null;
/*      */     try
/*      */     {
/* 1147 */       s = con.prepareStatement(stmt);
/* 1148 */       if ((params != null) && (params.length > 0)) {
/* 1149 */         for (int i = 0; i < params.length; i++) {
/* 1150 */           Param param = params[i];
/*      */ 
/* 1153 */           if (param == null)
/*      */             continue;
/* 1155 */           boolean nullable = true;
/* 1156 */           if (columnsNullable.containsKey(param.colName)) {
/* 1157 */             String colInfo = (String)columnsNullable.get(param.colName);
/* 1158 */             nullable = !colInfo.equalsIgnoreCase("NO");
/*      */           }
/*      */ 
/* 1161 */           if (Syntax.BINARY.equals(param.syntax)) {
/* 1162 */             if (param.value != null) {
/* 1163 */               Binary binary = (Binary)param.value;
/* 1164 */               if (!binary.isNull()) {
/* 1165 */                 byte[] bytes = binary.toDecodedByteArray();
/* 1166 */                 setBinaryParam(s, i + 1, bytes);
/*      */               }
/*      */               else {
/* 1169 */                 s.setBytes(i + 1, null);
/*      */               }
/*      */             } else {
/* 1172 */               s.setBytes(i + 1, null);
/*      */             }
/*      */           }
/* 1175 */           else if (Syntax.INT.equals(param.syntax)) {
/* 1176 */             if (param.value != null) {
/* 1177 */               Integer iObj = (Integer)Syntax.convertValue(param.syntax, param.value);
/* 1178 */               s.setInt(i + 1, iObj.intValue());
/*      */             } else {
/* 1180 */               s.setNull(i + 1, 4);
/*      */             }
/*      */           }
/* 1183 */           else if (Syntax.BOOLEAN.equals(param.syntax)) {
/* 1184 */             if (param.value != null) {
/* 1185 */               Boolean bObj = (Boolean)Syntax.convertValue(param.syntax, param.value);
/* 1186 */               s.setBoolean(i + 1, bObj.booleanValue());
/*      */             } else {
/* 1188 */               s.setNull(i + 1, 16);
/*      */             }
/*      */           }
/* 1191 */           else if (param.value != null) {
/* 1192 */             s.setString(i + 1, param.value.toString());
/*      */           }
/* 1195 */           else if (nullable) {
/* 1196 */             s.setString(i + 1, null);
/*      */           }
/*      */           else {
/* 1199 */             String empty = "";
/* 1200 */             s.setString(i + 1, empty);
/*      */           }
/*      */ 
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1209 */       s.execute();
/* 1210 */       flushResults(stmt, s);
/*      */     }
/*      */     catch (SQLException e) {
/* 1213 */       Resource resource = getResource();
/* 1214 */       Object value = null;
/* 1215 */       if (resource != null) {
/* 1216 */         value = resource.getResourceAttributeVal("rethrowAllSQLExceptions");
/*      */       }
/* 1218 */       boolean throwIt = (value != null) && (((Boolean)value).booleanValue());
/*      */ 
/* 1220 */       int code = e.getErrorCode();
/* 1221 */       if ((throwIt) || (code != ignoreError)) {
/* 1222 */         TRACE.throwing2("execStandardSqlWithParams", e);
/* 1223 */         throw new InternalError(e);
/*      */       }
/* 1225 */       if (TRACE.level3("execStandardSqlWithParams"))
/* 1226 */         TRACE.info(0L, "execStandardSqlWithParams", e.getMessage());
/*      */     }
/*      */     finally
/*      */     {
/* 1230 */       closeStatement(s);
/*      */     }
/* 1232 */     TRACE.exit2("execStandardSqlWithParams");
/*      */   }
/*      */ 
/*      */   protected void execOracleSqlWithParams(String id, Connection con, String stmt, Param[] params, HashMap columnsNullable, int ignoreError)
/*      */     throws WavesetException
/*      */   {
/* 1249 */     String METHOD = "execOracleSqlWithParams";
/* 1250 */     TRACE.entry2("execOracleSqlWithParams");
/* 1251 */     if (TRACE.level2("execOracleSqlWithParams")) {
/* 1252 */       StringBuffer sb = new StringBuffer(64);
/* 1253 */       sb.append("\nconnection=");
/* 1254 */       sb.append(getConnectionString(con));
/* 1255 */       sb.append("\nstatement=");
/* 1256 */       sb.append(stmt);
/* 1257 */       for (int i = 0; (params != null) && (i < params.length); i++) {
/* 1258 */         sb.append("\narg[" + (i + 1) + "]=");
/* 1259 */         sb.append(params[i].value);
/* 1260 */         sb.append(" [syntax=");
/* 1261 */         sb.append(params[i].syntax);
/* 1262 */         sb.append("]");
/*      */       }
/* 1264 */       sb.append("\nignoreError=");
/* 1265 */       sb.append(ignoreError);
/* 1266 */       TRACE.info2("execOracleSqlWithParams", sb.toString());
/*      */     }
/*      */ 
/* 1269 */     boolean needLobPass = false;
/* 1270 */     PreparedStatement s = null;
/* 1271 */     ResultSet rs = null;
/*      */ 
/* 1273 */     List lobInfo = new ArrayList();
/*      */     try
/*      */     {
/* 1281 */       con.setAutoCommit(false);
/*      */ 
/* 1283 */       s = con.prepareStatement(stmt);
/* 1284 */       if ((params != null) && (params.length > 0)) {
/* 1285 */         for (int i = 0; i < params.length; i++) {
/* 1286 */           Param param = params[i];
/* 1287 */           if (param == null)
/*      */             continue;
/* 1289 */           boolean nullable = true;
/* 1290 */           if (columnsNullable.containsKey(param.colName)) {
/* 1291 */             String colInfo = (String)columnsNullable.get(param.colName);
/* 1292 */             nullable = !colInfo.equalsIgnoreCase("NO");
/*      */           }
/* 1294 */           if (Syntax.BINARY.equals(param.syntax))
/*      */           {
/* 1298 */             if (param.value != null) {
/* 1299 */               Binary binary = (Binary)param.value;
/* 1300 */               if (!binary.isNull()) {
/* 1301 */                 int binaryColType = getColumnType(con, param.colName);
/* 1302 */                 if (2004 == binaryColType) {
/* 1303 */                   Map details = new HashMap();
/* 1304 */                   details.put("type", new Integer(binaryColType));
/* 1305 */                   details.put("param", param);
/* 1306 */                   lobInfo.add(details);
/* 1307 */                   needLobPass = true;
/* 1308 */                   s.setBlob(i + 1, OracleLobUtil.getEmptyBlob());
/*      */                 }
/* 1310 */                 else if (2005 == binaryColType) {
/* 1311 */                   Map details = new HashMap();
/* 1312 */                   details.put("type", new Integer(binaryColType));
/* 1313 */                   details.put("param", param);
/* 1314 */                   lobInfo.add(details);
/* 1315 */                   needLobPass = true;
/* 1316 */                   s.setClob(i + 1, OracleLobUtil.getEmptyClob());
/*      */                 }
/*      */                 else {
/* 1319 */                   byte[] bytes = binary.toDecodedByteArray();
/* 1320 */                   s.setBytes(i + 1, bytes);
/*      */                 }
/*      */               }
/*      */               else {
/* 1324 */                 s.setBytes(i + 1, null);
/*      */               }
/*      */             } else {
/* 1327 */               s.setBytes(i + 1, null);
/*      */             }
/*      */           }
/* 1330 */           else if (Syntax.INT.equals(param.syntax)) {
/* 1331 */             if (param.value != null) {
/* 1332 */               Integer iObj = (Integer)Syntax.convertValue(param.syntax, param.value);
/* 1333 */               s.setInt(i + 1, iObj.intValue());
/*      */             } else {
/* 1335 */               s.setNull(i + 1, 4);
/*      */             }
/*      */           }
/* 1338 */           else if (Syntax.BOOLEAN.equals(param.syntax)) {
/* 1339 */             if (param.value != null) {
/* 1340 */               Boolean bObj = (Boolean)Syntax.convertValue(param.syntax, param.value);
/* 1341 */               s.setBoolean(i + 1, bObj.booleanValue());
/*      */             }
/*      */             else
/*      */             {
/* 1347 */               s.setString(i + 1, null);
/*      */             }
/*      */           }
/* 1350 */           else if (param.value != null) {
/* 1351 */             s.setString(i + 1, param.value.toString());
/*      */           }
/* 1355 */           else if (nullable) {
/* 1356 */             s.setString(i + 1, null);
/*      */           } else {
/* 1358 */             String empty = "";
/* 1359 */             s.setString(i + 1, empty);
/*      */           }
/*      */ 
/*      */         }
/*      */ 
/*      */       }
/*      */ 
/* 1372 */       s.execute();
/* 1373 */       closeStatement(s);
/* 1374 */       s = null;
/*      */ 
/* 1376 */       if (needLobPass)
/*      */       {
/* 1382 */         StringBuffer columns = new StringBuffer();
/*      */ 
/* 1384 */         for (int i = 0; i < lobInfo.size(); i++) {
/* 1385 */           Map details = (Map)lobInfo.get(i);
/* 1386 */           Param param = (Param)details.get("param");
/* 1387 */           if (i > 0) {
/* 1388 */             columns.append(",");
/*      */           }
/* 1390 */           addQuotedName(columns, param.colName);
/*      */         }
/*      */ 
/* 1393 */         String sql = "SELECT " + columns.toString() + " from " + getRequiredResAttrVal("Table") + " WHERE " + getRequiredResAttrVal("Key Column") + " = ? FOR UPDATE";
/*      */ 
/* 1395 */         s = con.prepareStatement(sql);
/* 1396 */         s.setString(1, id);
/* 1397 */         rs = s.executeQuery();
/* 1398 */         rs.next();
/*      */ 
/* 1400 */         StringBuffer setClause = new StringBuffer();
/* 1401 */         setClause.append("SET ");
/*      */ 
/* 1403 */         for (int i = 0; i < lobInfo.size(); i++) {
/* 1404 */           Map details = (Map)lobInfo.get(i);
/* 1405 */           Param param = (Param)details.get("param");
/* 1406 */           if (i > 0) {
/* 1407 */             setClause.append(",");
/*      */           }
/* 1409 */           addQuotedName(setClause, param.colName);
/* 1410 */           setClause.append(" = ?");
/* 1411 */           Integer type = (Integer)details.get("type");
/* 1412 */           if (type.intValue() == 2004) {
/* 1413 */             Object blob = rs.getObject(i + 1);
/* 1414 */             OutputStream bos = OracleLobUtil.getBlobBinaryOutputStream(blob);
/* 1415 */             byte[] bytes = ((Binary)param.value).toDecodedByteArray();
/* 1416 */             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
/*      */             try {
/* 1418 */               Util.copyFile(bais, bos);
/*      */ 
/* 1420 */               bos.close();
/*      */             }
/*      */             catch (IOException e) {
/* 1423 */               WavesetException we = new WavesetException(e);
/* 1424 */               TRACE.throwing2("execOracleSqlWithParams", we);
/* 1425 */               throw we;
/*      */             }
/* 1427 */             details.put("lob", blob);
/*      */           }
/*      */           else {
/* 1430 */             Object clob = rs.getObject(i + 1);
/* 1431 */             OutputStream bos = OracleLobUtil.getClobBinaryOutputStream(clob);
/* 1432 */             byte[] bytes = ((Binary)param.value).toDecodedByteArray();
/* 1433 */             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
/*      */             try {
/* 1435 */               Util.copyFile(bais, bos);
/*      */ 
/* 1437 */               bos.close();
/*      */             }
/*      */             catch (IOException e) {
/* 1440 */               WavesetException we = new WavesetException(e);
/* 1441 */               TRACE.throwing2("execOracleSqlWithParams", we);
/* 1442 */               throw we;
/*      */             }
/* 1444 */             details.put("lob", clob);
/*      */           }
/*      */         }
/*      */ 
/* 1448 */         flushResults(sql, s);
/*      */ 
/* 1451 */         closeResult(rs);
/* 1452 */         rs = null;
/* 1453 */         closeStatement(s);
/* 1454 */         s = null;
/*      */ 
/* 1456 */         sql = "UPDATE " + getRequiredResAttrVal("Table") + " " + setClause.toString() + " WHERE " + getRequiredResAttrVal("Key Column") + " = ?";
/*      */ 
/* 1459 */         s = con.prepareStatement(sql);
/*      */ 
/* 1461 */         for (int i = 0; i < lobInfo.size(); i++) {
/* 1462 */           Map details = (Map)lobInfo.get(i);
/* 1463 */           Object val = details.get("lob");
/* 1464 */           s.setObject(i + 1, val);
/*      */         }
/* 1466 */         s.setString(lobInfo.size() + 1, id);
/*      */ 
/* 1468 */         s.executeUpdate();
/*      */ 
/* 1470 */         flushResults(stmt, s);
/*      */       }
/*      */ 
/* 1477 */       con.commit();
/*      */     }
/*      */     catch (SQLException e)
/*      */     {
/* 1481 */       Resource resource = getResource();
/* 1482 */       Object value = null;
/* 1483 */       if (resource != null) {
/* 1484 */         value = resource.getResourceAttributeVal("rethrowAllSQLExceptions");
/*      */       }
/* 1486 */       boolean throwIt = (value != null) && (((Boolean)value).booleanValue());
/*      */ 
/* 1488 */       int code = e.getErrorCode();
/* 1489 */       if ((throwIt) || (code != ignoreError)) {
/* 1490 */         TRACE.throwing2("execOracleSqlWithParams", e);
/* 1491 */         throw new InternalError(e);
/*      */       }
/* 1493 */       if (TRACE.level3("execOracleSqlWithParams"))
/* 1494 */         TRACE.info(0L, "execOracleSqlWithParams", e.getMessage());
/*      */     }
/*      */     finally
/*      */     {
/* 1498 */       closeResult(rs);
/* 1499 */       rs = null;
/* 1500 */       closeStatement(s);
/* 1501 */       s = null;
/*      */     }
/* 1503 */     TRACE.exit2("execOracleSqlWithParams");
/*      */   }
/*      */ 
/*      */   private HashMap getColumnsNullable(Connection con)
/*      */     throws WavesetException
/*      */   {
/* 1511 */     Resource resource = getResource();
/* 1512 */     AccountAttributeType[] attrs = resource.getAccountAttributeTypes();
/*      */ 
/* 1515 */     HashMap nullables = new HashMap();
/* 1516 */     for (int i = 0; i < attrs.length; i++)
/*      */     {
/* 1518 */       nullables.put(attrs[i].getMapName(), "");
/*      */     }
/*      */ 
/* 1521 */     Boolean empty = getOptionalBooleanResAttrVal("enableEmptyString");
/* 1522 */     if (!empty.booleanValue()) {
/* 1523 */       return nullables;
/*      */     }
/*      */ 
/* 1526 */     ResultSet res = null;
/*      */     try
/*      */     {
/* 1529 */       String table = (String)getRequiredResAttrVal("Table");
/* 1530 */       res = con.getMetaData().getColumns(null, null, table, null);
/*      */ 
/* 1532 */       if (res != null)
/* 1533 */         while (res.next()) {
/* 1534 */           String colName = res.getString("column_name");
/*      */ 
/* 1536 */           if ((colName == null) || (!nullables.containsKey(colName))) {
/*      */             continue;
/*      */           }
/* 1539 */           String nullable = res.getString("is_nullable");
/* 1540 */           nullables.put(colName, nullable);
/*      */         }
/*      */     }
/*      */     catch (SQLException e)
/*      */     {
/* 1545 */       throw new WavesetException(e);
/*      */     }
/*      */     finally {
/* 1548 */       closeResult(res);
/* 1549 */       res = null;
/*      */     }
/* 1551 */     return nullables;
/*      */   }
/*      */   private int getColumnType(Connection con, String columnName) throws WavesetException {
/* 1568 */     ResultSet rs = null;
/* 1569 */     PreparedStatement st = null;
/*      */     int sqlType;
/*      */     try { st = con.prepareStatement("SELECT " + columnName + " FROM " + getRequiredResAttrVal("Table") + " WHERE " + getRequiredResAttrVal("Key Column") + "=NULL");
/*      */ 
/* 1575 */       rs = st.executeQuery();
/* 1576 */       sqlType = rs.getMetaData().getColumnType(1);
/*      */     } catch (SQLException e)
/*      */     {
/* 1579 */       throw new WavesetException(e);
/*      */     }
/*      */     finally {
/* 1582 */       closeResult(rs);
/* 1583 */       rs = null;
/* 1584 */       closeStatement(st);
/* 1585 */       st = null;
/*      */     }
/* 1587 */     return sqlType;
/*      */   }
/*      */ 
/*      */   public AccountIterator getAccountIterator()
/*      */     throws WavesetException
/*      */   {
/* 1601 */     return getAccountIterator(null);
/*      */   }
/*      */ 
/*      */   public AccountIterator getAccountIterator(Map searchParams) throws WavesetException
/*      */   {
/* 1606 */     String METHOD = "getAccountIterator";
/*      */ 
/* 1608 */     TRACE.entry1("getAccountIterator");
/* 1609 */     if (TRACE.level1("getAccountIterator")) {
/* 1610 */       String msg = null;
/* 1611 */       if (searchParams != null) {
/* 1612 */         msg = searchParams.toString();
/*      */       }
/* 1614 */       TRACE.info1("getAccountIterator", msg);
/*      */     }
/*      */ 
/* 1617 */     Connection con = getConnection();
/* 1618 */     AccountIterator acctIter = new DatabaseTableAccountIterator(con, this, searchParams);
/*      */ 
/* 1621 */     TRACE.exit1("getAccountIterator", acctIter);
/*      */ 
/* 1623 */     return acctIter;
/*      */   }
/*      */ 
/*      */   public GenericObject getFeatures()
/*      */   {
/* 1905 */     GenericObject result = super.getFeatures();
/*      */ 
/* 1907 */     Resource resource = getResource();
/* 1908 */     if (resource != null)
/*      */     {
/* 1910 */       Object value = resource.getResourceAttributeVal("noPasswords");
/* 1911 */       if ((value != null) && (((Boolean)value).booleanValue()))
/*      */       {
/* 1913 */         result.remove("account.PASSWORD");
/*      */       }
/*      */     }
/*      */ 
/* 1917 */     result.put("account.BINARY", "account.BINARY");
/* 1918 */     result.put("account.RENAME", "account.RENAME");
/*      */ 
/* 1920 */     return result;
/*      */   }
/*      */ 
/*      */   public void init()
/*      */     throws Exception
/*      */   {
/* 1939 */     String METHOD = "init";
/* 1940 */     TRACE.entry1("init");
/*      */ 
/* 1942 */     this._util = new ActiveSyncUtil(getResource(), this);
/* 1943 */     traceAndLog(1L, "init", 2, "Initializing DatabaseTable resource adapter.\n");
/*      */ 
/* 1946 */     traceAndLog(1L, "init", 2, this._resource.toXml());
/* 1947 */     traceAndLog(1L, "init", 2, "Done initializing DatabaseTable resource adapter.\n");
/*      */ 
/* 1949 */     TRACE.exit1("init");
/*      */   }
/*      */ 
/*      */   public void shutdown()
/*      */   {
/*      */   }
/*      */ 
/*      */   public Object getAttributeValue(String name)
/*      */     throws WavesetException
/*      */   {
/* 1965 */     return getResource().getResourceAttributeVal(name);
/*      */   }
/*      */ 
/*      */   public void setAttributeValue(String name, Object value)
/*      */     throws WavesetException
/*      */   {
/* 1974 */     getResource().setResourceAttributeVal(name, value);
/*      */   }
/*      */ 
/*      */   public int poll()
/*      */   {
/* 1986 */     String METHOD = "poll";
/* 1987 */     TRACE.entry1("poll");
/* 1988 */     StringBuffer tsb = new StringBuffer(48);
/* 1989 */     tsb.append("Starting poll for ");
/* 1990 */     tsb.append(getResource().getName());
/* 1991 */     tsb.append(" resource adapter.\n");
/* 1992 */     traceAndLog(1L, "poll", 2, tsb.toString());
/*      */ 
/* 1994 */     int numProcessed = 0;
/*      */     try
/*      */     {
/* 2001 */       IAPI prox = IAPIFactory.getIAPIProcess(null, null, null, this);
/* 2002 */       Map lastProcessedMap = ActiveSyncUtil.reconstituteEmptyStrings((Map)prox.getProperty("lastProcessed"));
/*      */ 
/* 2004 */       if (TRACE.level1("poll")) {
/* 2005 */         StringBuffer sb = new StringBuffer("lastProcessed map = ");
/* 2006 */         sb.append(lastProcessedMap);
/* 2007 */         TRACE.info(0L, "poll", sb.toString());
/*      */       }
/* 2009 */       if (this._util.getLogLevel() >= 2) {
/* 2010 */         this._util.logInfoMap("lastProcessed", lastProcessedMap);
/*      */       }
/*      */ 
/* 2017 */       UpdateRows updateRows = getUpdatedRows(lastProcessedMap);
/*      */ 
/* 2019 */       if ((updateRows != null) && (updateRows.getUpdateRows() != null) && (updateRows.getUpdateRows().size() > 0))
/*      */       {
/* 2023 */         List updates = updateRows.getUpdateRows();
/* 2024 */         numProcessed = updates.size();
/*      */ 
/* 2026 */         processUpdates(updateRows, "update", prox);
/*      */       }
/*      */ 
/* 2029 */       if (numProcessed == 0) {
/* 2030 */         traceAndLog(1L, "poll", 2, "Poll found nothing to do");
/*      */       }
/*      */     }
/*      */     catch (Throwable th)
/*      */     {
/* 2035 */       this._util.logResourceException("Error processing updates", th, TRACE, "poll");
/*      */     }
/*      */ 
/* 2038 */     traceAndLog(1L, "poll", 2, "Completed poll for " + getResource().getName() + " resource adapter.\n");
/*      */ 
/* 2041 */     TRACE.exit1("poll", numProcessed);
/*      */ 
/* 2043 */     return numProcessed;
/*      */   }
/*      */ 
/*      */   public void callCompleted(IAPI call)
/*      */   {
/* 2060 */     this._util.logString(2, "Completed IAPI " + call.toXml());
/*      */   }
/*      */ 
/*      */   UpdateRows getUpdatedRows(Map lastProcessedMap)
/*      */   {
/* 2078 */     String METHOD = "getUpdatedRows";
/* 2079 */     TRACE.entry2("getUpdatedRows");
/* 2080 */     if (TRACE.level2("getUpdatedRows")) {
/* 2081 */       StringBuffer sb = new StringBuffer(80);
/* 2082 */       sb.append("lastProcessedMap=");
/* 2083 */       sb.append(lastProcessedMap);
/* 2084 */       TRACE.info2("getUpdatedRows", sb.toString());
/*      */     }
/* 2086 */     List list = new ArrayList();
/*      */ 
/* 2088 */     UpdateRows updateRows = new UpdateRows(list, new UpdateRow(null, lastProcessedMap));
/*      */     try
/*      */     {
/* 2092 */       HashMap listOptions = new HashMap();
/*      */ 
/* 2101 */       StringBuffer qualifierBuffer = new StringBuffer();
/* 2102 */       String staticSearchPredicate = getOptionalStringResAttrVal("Static Search Predicate");
/* 2103 */       String lastFetchedConjunction = getOptionalStringResAttrVal("Last Fetched Conjunction");
/* 2104 */       String lastFetchedPredicate = getOptionalStringResAttrVal("Last Fetched Predicate");
/*      */ 
/* 2106 */       boolean needConj = false;
/* 2107 */       if (Util.length(staticSearchPredicate) > 0) {
/* 2108 */         qualifierBuffer.append(staticSearchPredicate);
/* 2109 */         needConj = true;
/*      */       }
/*      */ 
/* 2112 */       boolean isLastProcessedEmpty = (lastProcessedMap == null) || (lastProcessedMap.size() == 0);
/* 2113 */       if ((isLastProcessedEmpty) && (Util.length(lastFetchedPredicate) > 0))
/*      */       {
/* 2118 */         traceAndLog(1L, "getUpdatedRows", 2, "Last Fetched Predicate ignored this initial poll because there is no lastProcessed property set");
/*      */       }
/*      */ 
/* 2128 */       if ((!isLastProcessedEmpty) && (Util.length(lastFetchedPredicate) > 0))
/*      */       {
/* 2133 */         String expandedLastFetchedPredicate = VariableExpander.expand(lastFetchedPredicate, new StrictMapResolver(lastProcessedMap));
/*      */ 
/* 2136 */         if (needConj) {
/* 2137 */           qualifierBuffer.append(" ");
/* 2138 */           qualifierBuffer.append(lastFetchedConjunction);
/* 2139 */           qualifierBuffer.append(" ");
/*      */         }
/* 2141 */         qualifierBuffer.append(expandedLastFetchedPredicate);
/*      */       }
/*      */ 
/* 2147 */       listOptions.put("searchFilter", qualifierBuffer.toString());
/*      */ 
/* 2152 */       String orderByClause = getOptionalStringResAttrVal("Order By Clause");
/* 2153 */       if (Util.length(orderByClause) > 0) {
/* 2154 */         listOptions.put("orderBy", orderByClause);
/*      */       }
/*      */ 
/* 2158 */       if (TRACE.level1("getUpdatedRows")) {
/* 2159 */         StringBuffer sb = new StringBuffer(64);
/* 2160 */         sb.append("listOptions map = ");
/* 2161 */         sb.append(listOptions);
/* 2162 */         TRACE.info(0L, "getUpdatedRows", sb.toString());
/*      */       }
/*      */ 
/* 2165 */       if (this._util.getLogLevel() >= 2) {
/* 2166 */         this._util.logInfoMap("listOptions", listOptions);
/*      */       }
/*      */ 
/* 2172 */       AccountIterator acctIter = null;
/*      */       try {
/* 2174 */         acctIter = getAccountIterator(listOptions);
/*      */ 
/* 2176 */         while ((acctIter.hasNext()) && (!this._util.isStopRequested())) {
/* 2177 */           WSUser user = acctIter.next();
/* 2178 */           Map userMap = user.toHashMap(true);
/* 2179 */           ResourceInfo ri = user.getResourceInfo(getResource());
/* 2180 */           if (ri != null) {
/* 2181 */             userMap.put("identity", ri.getAccountId());
/*      */           }
/* 2183 */           list.add(new UpdateRow(null, userMap));
/*      */         }
/*      */       } finally {
/* 2186 */         if (acctIter != null)
/* 2187 */           acctIter.close();
/*      */       }
/*      */     }
/*      */     catch (WavesetException ex) {
/* 2191 */       this._util.logString(1, Util.stackToString(ex));
/*      */     }
/*      */ 
/* 2194 */     TRACE.exit2("getUpdatedRows", updateRows);
/* 2195 */     return updateRows;
/*      */   }
/*      */ 
/*      */   protected IAPI buildEventLegacy(UpdateRow row, String command)
/*      */     throws WavesetException, IAPIException
/*      */   {
/* 2230 */     String METHOD = "buildEvent";
/* 2231 */     TRACE.entry2("buildEvent");
/* 2232 */     if (TRACE.level2("buildEvent")) {
/* 2233 */       StringBuffer sb = new StringBuffer(48);
/* 2234 */       sb.append("row=");
/* 2235 */       if (row != null)
/* 2236 */         sb.append(row.toString());
/*      */       else {
/* 2238 */         sb.append("null");
/*      */       }
/* 2240 */       sb.append("command=");
/* 2241 */       sb.append(command);
/* 2242 */       TRACE.info2("buildEvent", sb.toString());
/*      */     }
/*      */ 
/* 2249 */     Map userMap = row.getUserMap();
/* 2250 */     String identity = (String)userMap.get("identity");
/* 2251 */     if (identity == null) {
/* 2252 */       String msg = "Missing required attribute identity. This is a configuration problem.";
/*      */ 
/* 2254 */       WavesetException we = new WavesetException("Missing required attribute identity. This is a configuration problem.", Severity.ERROR);
/* 2255 */       TRACE.throwing2("buildEvent", we);
/* 2256 */       throw we;
/*      */     }
/*      */ 
/* 2263 */     Map options = new HashMap();
/* 2264 */     String adminName = (String)userMap.get("AUDIT_OPRID");
/* 2265 */     if (adminName != null) {
/* 2266 */       options.put("RemoteAdminName", adminName);
/*      */     }
/* 2268 */     IAPI update = null;
/*      */ 
/* 2274 */     String processName = getOptionalStringResAttrVal("processRule");
/* 2275 */     if (processName != null)
/*      */     {
/* 2279 */       IAPIProcess process = IAPIFactory.getIAPIProcess(options, userMap, "create", this);
/*      */ 
/* 2281 */       process.setProcessName(processName);
/* 2282 */       update = process;
/*      */     }
/*      */     else
/*      */     {
/* 2288 */       update = IAPIFactory.getIAPIUser(options, userMap, command, this);
/*      */ 
/* 2294 */       List viewIDs = update.find(Attribute.NAME.toString(), identity, 2, false);
/* 2295 */       if ((viewIDs == null) || (viewIDs.size() < 1)) {
/* 2296 */         viewIDs = update.find(Attribute.RESOURCE_ACCOUNT_IDS.toString(), identity, 2, false);
/*      */       }
/*      */ 
/* 2299 */       if (viewIDs == null)
/*      */       {
/* 2303 */         if ("update".equals(command)) {
/* 2304 */           update.setCommand("create");
/* 2305 */           StringBuffer sb = new StringBuffer(64);
/* 2306 */           sb.append("Received update for user ");
/* 2307 */           sb.append(identity);
/* 2308 */           sb.append(" not in Lighthouse. Turning into a create\n");
/* 2309 */           traceAndLog(1L, "buildEvent", 2, sb.toString());
/*      */         }
/*      */       }
/*      */       else {
/* 2313 */         if (viewIDs.size() > 1) {
/* 2314 */           StringBuffer sb = new StringBuffer("Found multiple matches for id ");
/* 2315 */           sb.append(userMap.get("name"));
/* 2316 */           InternalError ie = new InternalError(sb.toString());
/*      */ 
/* 2318 */           TRACE.throwing2("buildEvent", ie);
/* 2319 */           throw ie;
/*      */         }
/*      */ 
/* 2322 */         update.setViewID((String)viewIDs.get(0));
/*      */       }
/*      */ 
/*      */     }
/*      */ 
/* 2329 */     update.setLogger(this._util.getLogger());
/*      */ 
/* 2331 */     TRACE.exit2("buildEvent", update);
/* 2332 */     return update;
/*      */   }
/*      */ 
/*      */   protected boolean useLegacyEventGeneration()
/*      */   {
/* 2340 */     Boolean legacy = getOptionalBooleanResAttrVal("useLegacyEventGeneration");
/*      */ 
/* 2342 */     return (legacy != null) && (legacy.booleanValue());
/*      */   }
/*      */ 
/*      */   private boolean ignoreEventFailures()
/*      */   {
/* 2349 */     return getOptionalBooleanResAttrVal("ignoreEventFailures").booleanValue();
/*      */   }
/*      */ 
/*      */   protected void processUpdates(UpdateRows updateRows, String command, IAPI prox)
/*      */     throws WavesetException, IAPIException
/*      */   {
/* 2360 */     String METHOD = "processUpdates";
/* 2361 */     TRACE.entry2("processUpdates");
/* 2362 */     if (TRACE.level2("processUpdates")) {
/* 2363 */       StringBuffer sb = new StringBuffer(64);
/* 2364 */       sb.append("updateRows=");
/* 2365 */       sb.append(updateRows);
/* 2366 */       sb.append("command=");
/* 2367 */       sb.append(command);
/* 2368 */       sb.append("prox=");
/* 2369 */       sb.append(prox);
/* 2370 */       TRACE.info2("processUpdates", sb.toString());
/*      */     }
/* 2372 */     List list = updateRows.getUpdateRows();
/* 2373 */     if (list != null) {
/* 2374 */       Iterator it = list.iterator();
/* 2375 */       for (int i = 0; (it.hasNext()) && (!this._util.isStopRequested()); i++) {
/* 2376 */         UpdateRow row = (UpdateRow)it.next();
/*      */ 
/* 2378 */         traceAndLogCommand(3L, "processUpdates", 2, row, command, null);
/*      */ 
/* 2381 */         IAPI event = null;
/* 2382 */         if (useLegacyEventGeneration()) {
/* 2383 */           event = buildEventLegacy(row, command);
/*      */         } else {
/* 2385 */           Map options = new HashMap();
/* 2386 */           Map userMap = row.getUserMap();
/* 2387 */           String adminName = (String)userMap.get("AUDIT_OPRID");
/* 2388 */           if (adminName != null)
/* 2389 */             options.put("RemoteAdminName", adminName);
/* 2390 */           event = IAPIFactory.getIAPI(options, userMap, this);
/*      */         }
/*      */ 
/* 2393 */         if (event != null) {
/* 2394 */           WavesetResult result = event.submit();
/*      */ 
/* 2396 */           if (!result.hasError())
/*      */           {
/* 2401 */             prox.setProperty("lastProcessed", row.getUserMap());
/*      */           }
/* 2405 */           else if (ignoreEventFailures())
/*      */           {
/* 2412 */             traceAndLog(3L, "processUpdates", 2, "Updating lastProcessed with current map even though an error was detected");
/*      */ 
/* 2415 */             prox.setProperty("lastProcessed", row.getUserMap());
/*      */           }
/*      */ 
/* 2420 */           traceAndLogCommand(3L, "processUpdates", 2, row, command, result);
/*      */         }
/*      */       }
/*      */ 
/*      */     }
/*      */ 
/* 2426 */     TRACE.exit1("processUpdates");
/*      */   }
/*      */ 
/*      */   protected void traceAndLogCommand(long traceLevel, String method, int logLevel, UpdateRow update, String command, WavesetResult result)
/*      */   {
/* 2437 */     if (update == null) {
/* 2438 */       return;
/*      */     }
/*      */ 
/* 2441 */     if ((!TRACE.isLogging(traceLevel, method)) && (this._util.getLogLevel() < logLevel))
/*      */     {
/* 2445 */       return;
/*      */     }
/*      */ 
/* 2448 */     Map audit = update.getAuditMap();
/* 2449 */     Map user = update.getUserMap();
/* 2450 */     List msgs = result != null ? result.getMessages() : null;
/* 2451 */     List errs = result != null ? result.getErrors() : null;
/*      */ 
/* 2453 */     StringBuffer sb = new StringBuffer();
/* 2454 */     if (result == null) {
/* 2455 */       sb.append("Starting ");
/* 2456 */       sb.append(command);
/* 2457 */       sb.append(" processing for resource ");
/* 2458 */       sb.append(getResource().getName());
/* 2459 */       sb.append('\n');
/* 2460 */       if (TRACE.isLogging(traceLevel, method)) {
/* 2461 */         TRACE.info(traceLevel, method, sb.toString());
/*      */       }
/* 2463 */       if (logLevel < this._util.getLogLevel()) {
/* 2464 */         this._util.logString(logLevel, sb.toString());
/*      */       }
/* 2466 */       sb.setLength(0);
/*      */     }
/*      */ 
/* 2469 */     ActiveSyncUtil.mapToStringBuffer(audit, sb);
/* 2470 */     ActiveSyncUtil.mapToStringBuffer(user, sb);
/* 2471 */     ActiveSyncUtil.listToStringBuffer(errs, sb);
/* 2472 */     ActiveSyncUtil.listToStringBuffer(msgs, sb);
/*      */     try
/*      */     {
/* 2475 */       if (TRACE.isLogging(traceLevel, method)) {
/* 2476 */         TRACE.info(traceLevel, method, sb.toString());
/*      */       }
/* 2478 */       if (logLevel < this._util.getLogLevel())
/* 2479 */         this._util.logString(logLevel, sb.toString());
/*      */     }
/*      */     catch (Throwable th) {
/* 2482 */       System.out.println("DatabaseTable Resource adapter. Error writing log: " + th.toString());
/*      */     }
/*      */ 
/* 2485 */     if (result != null) {
/* 2486 */       sb.setLength(0);
/* 2487 */       sb.append("End update processing for resource ");
/* 2488 */       sb.append(getResource().getName());
/* 2489 */       sb.append('\n');
/* 2490 */       if (TRACE.isLogging(traceLevel, method)) {
/* 2491 */         TRACE.info(traceLevel, method, sb.toString());
/*      */       }
/* 2493 */       if (logLevel < this._util.getLogLevel())
/* 2494 */         this._util.logString(logLevel, sb.toString());
/*      */     }
/*      */   }
/*      */ 
/*      */   public WavesetResult activeSyncTestConfiguration()
/*      */     throws WavesetException
/*      */   {
/* 2501 */     String METHOD = "activeSyncTestConfiguration";
/* 2502 */     TRACE.entry1("activeSyncTestConfiguration");
/* 2503 */     WavesetResult result = new WavesetResult();
/*      */ 
/* 2510 */     String staticSearchFilter = getOptionalStringResAttrVal("Static Search Predicate");
/* 2511 */     String lastFetchedFilter = getOptionalStringResAttrVal("Last Fetched Predicate");
/* 2512 */     if ((Util.length(staticSearchFilter) == 0) && (Util.length(lastFetchedFilter) == 0)) {
/* 2513 */       Message errmsg = new Message("com.waveset.adapter.RAMessages:DBTABLE_SYNC_TESTCONFIG_NO_PREDICATE", "Static Search Predicate", "Last Fetched Predicate");
/*      */ 
/* 2515 */       TRACE.info3("activeSyncTestConfiguration", errmsg.getMessage());
/* 2516 */       result.addWarning(errmsg);
/*      */     }
/*      */ 
/* 2522 */     String lastFetchedConjunction = getOptionalStringResAttrVal("Last Fetched Conjunction");
/* 2523 */     if (Util.length(lastFetchedConjunction) > 0) {
/* 2524 */       if ((!lastFetchedConjunction.equalsIgnoreCase("AND")) && (!lastFetchedConjunction.equalsIgnoreCase("OR")))
/*      */       {
/* 2526 */         Object[] parms = { lastFetchedConjunction, "Last Fetched Conjunction" };
/* 2527 */         ErrorMessage errmsg = new ErrorMessage(Severity.ERROR, "com.waveset.adapter.RAMessages:DBTABLE_SYNC_TESTCONFIG_BAD_CONJUNCTION", parms);
/*      */ 
/* 2530 */         TRACE.info3("activeSyncTestConfiguration", errmsg.getMessage());
/* 2531 */         result.addError(errmsg);
/*      */       }
/*      */ 
/*      */     }
/* 2535 */     else if ((Util.length(staticSearchFilter) > 0) && (Util.length(lastFetchedFilter) > 0)) {
/* 2536 */       ErrorMessage errmsg = new ErrorMessage(Severity.ERROR, "com.waveset.adapter.RAMessages:DBTABLE_SYNC_TESTCONFIG_MISSING_ATTR", "Last Fetched Conjunction");
/*      */ 
/* 2540 */       TRACE.info3("activeSyncTestConfiguration", errmsg.getMessage());
/* 2541 */       result.addError(errmsg);
/*      */     }
/*      */ 
/* 2549 */     if (Util.length(lastFetchedFilter) > 0) {
/* 2550 */       VariableChecker checker = new VariableChecker(getSchemaMap());
/*      */       try {
/* 2552 */         VariableExpander.expand(lastFetchedFilter, checker);
/*      */       }
/*      */       catch (WavesetException e) {
/* 2555 */         TRACE.info3("activeSyncTestConfiguration", e.getMessage());
/* 2556 */         result.addException(e);
/*      */       }
/*      */     }
/* 2559 */     TRACE.exit1("activeSyncTestConfiguration", result);
/* 2560 */     return result;
/*      */   }
/*      */ 
/*      */   protected void traceAndLog(long traceLevel, String method, int logLevel, String message)
/*      */   {
/* 2662 */     TRACE.info(traceLevel, method, message);
/* 2663 */     this._util.logString(logLevel, message);
/*      */   }
/*      */ 
/*      */   public void addQuotedName(StringBuffer b, String s)
/*      */   {
/* 2694 */     String quoting = getOptionalStringResAttrVal("Quoting");
/* 2695 */     addQuotedName(b, s, quoting);
/*      */   }
/*      */ 
/*      */   public static void addQuotedName(StringBuffer b, String s, String quoting) {
/* 2699 */     if ((b == null) || (s == null))
/* 2700 */       return;
/* 2701 */     if ((quoting == null) || (quoting.equals("none"))) {
/* 2702 */       b.append(s);
/* 2703 */     } else if (quoting.equalsIgnoreCase("Double")) {
/* 2704 */       b.append("\"");
/* 2705 */       b.append(s);
/*      */ 
/* 2717 */       b.append("\"");
/* 2718 */     } else if (quoting.equalsIgnoreCase("Single")) {
/* 2719 */       b.append("'");
/* 2720 */       b.append(s);
/* 2721 */       b.append("'");
/* 2722 */     } else if (quoting.equalsIgnoreCase("Back")) {
/* 2723 */       b.append("`");
/* 2724 */       b.append(s);
/* 2725 */       b.append("`");
/* 2726 */     } else if (quoting.equalsIgnoreCase("Brackets")) {
/* 2727 */       b.append("[");
/* 2728 */       b.append(s);
/* 2729 */       b.append("]");
/*      */     } else {
/* 2731 */       b.append(s);
/*      */     }
/*      */   }
/*      */ 
/*      */   public Connection getConnection()
/*      */     throws WavesetException
/*      */   {
/* 2740 */     Connection conn = super.getConnection();
/* 2741 */     cleanupConnection(conn);
/* 2742 */     return conn;
/*      */   }
/*      */ 
/*      */   private void cleanupConnection(Connection con)
/*      */     throws WavesetException
/*      */   {
/* 2749 */     String METHOD = "cleanupConnection";
/*      */ 
/* 2751 */     if (con != null)
/*      */     {
/* 2753 */       boolean isAutoCommit = true;
/*      */       try {
/* 2755 */         isAutoCommit = con.getAutoCommit();
/*      */       } catch (SQLException t) {
/* 2757 */         if (TRACE.level2("cleanupConnection")) {
/* 2758 */           TRACE.info2("cleanupConnection", "Failed to call getAutoCommit() on " + getConnectionString(con) + ": " + t.toString());
/*      */         }
/*      */ 
/* 2762 */         throw new WavesetException(t);
/*      */       }
/*      */ 
/* 2765 */       if (!isAutoCommit) {
/*      */         try {
/* 2767 */           con.rollback();
/*      */         } catch (SQLException t) {
/* 2769 */           if (TRACE.level2("cleanupConnection")) {
/* 2770 */             TRACE.info2("cleanupConnection", "Failed to rollback a transacted connection " + getConnectionString(con) + ": " + t.toString());
/*      */           }
/*      */ 
/* 2774 */           throw new WavesetException(t);
/*      */         }
/*      */         try
/*      */         {
/* 2778 */           if (TRACE.level4("cleanupConnection")) {
/* 2779 */             TRACE.info4("cleanupConnection", "Forcing connection " + getConnectionString(con) + " back to autoCommit(true)");
/*      */           }
/* 2781 */           con.setAutoCommit(true);
/*      */         } catch (SQLException t) {
/* 2783 */           if (TRACE.level2("cleanupConnection")) {
/* 2784 */             TRACE.info2("cleanupConnection", "Failed to set " + getConnectionString(con) + " auto commit to true: " + t.toString());
/*      */           }
/*      */ 
/* 2788 */           throw new WavesetException(t);
/*      */         }
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public void closeConnection(Connection con)
/*      */   {
/* 2799 */     String METHOD = "closeConnection";
/*      */ 
/* 2801 */     TRACE.entry1("closeConnection");
/* 2802 */     if (con != null) {
/*      */       try {
/* 2804 */         if (!con.getAutoCommit()) {
/* 2805 */           con.rollback();
/* 2806 */           con.setAutoCommit(true);
/*      */         }
/* 2808 */         super.closeConnection(con);
/*      */       } catch (SQLException e) {
/* 2810 */         if (TRACE.level3("closeConnection")) {
/* 2811 */           StringBuffer sb = new StringBuffer();
/* 2812 */           sb.append("WARNING: Exception closing JDBC connection.  Will destroy connection instead.");
/* 2813 */           sb.append(e.getMessage());
/* 2814 */           TRACE.info(0L, "closeConnection", sb.toString());
/*      */         }
/* 2816 */         destroyConnection(con);
/*      */       }
/*      */     }
/* 2819 */     TRACE.exit1("closeConnection");
/*      */   }
/*      */ 
/*      */   private void destroyConnection(Connection con) {
/* 2823 */     String METHOD = "destroyConnection";
/* 2824 */     TRACE.entry1("destroyConnection");
/* 2825 */     if (TRACE.level1("destroyConnection")) {
/* 2826 */       StringBuffer sb = new StringBuffer(48);
/* 2827 */       sb.append("connection=");
/* 2828 */       sb.append(getConnectionString(con));
/* 2829 */       TRACE.info1("destroyConnection", sb.toString());
/*      */     }
/*      */     try
/*      */     {
/* 2833 */       if ((con instanceof PooledConnection))
/*      */       {
/* 2837 */         PooledConnection pc = (PooledConnection)con;
/* 2838 */         pc.destroy();
/*      */       }
/*      */       else
/*      */       {
/*      */         try
/*      */         {
/* 2847 */           if (!con.getAutoCommit())
/* 2848 */             con.rollback();
/*      */         }
/*      */         catch (SQLException e)
/*      */         {
/* 2852 */           TRACE.caught(0L, "destroyConnection", e);
/*      */         }
/*      */ 
/* 2856 */         con.close();
/*      */       }
/*      */     }
/*      */     catch (SQLException e) {
/* 2860 */       TRACE.caught(0L, "destroyConnection", e);
/*      */     }
/*      */ 
/* 2863 */     TRACE.exit1("destroyConnection");
/*      */   }
/*      */ 
/*      */   public boolean checkNativeTimestamps(Resource resource, ResultSet rs) throws SQLException
/*      */   {
/* 2868 */     boolean nativeTimestampsFlag = false;
/* 2869 */     if (this._nativeTimestampsMap == null) {
/* 2870 */       Object nativeTsVal = resource.getResourceAttributeVal("nativeTimestamps");
/* 2871 */       if ((nativeTsVal != null) && (nativeTsVal.toString() != null) && (nativeTsVal.toString().equalsIgnoreCase("true")))
/*      */       {
/* 2874 */         this._nativeTimestampsMap = new HashMap();
/* 2875 */         ResultSetMetaData rsmd = rs.getMetaData();
/* 2876 */         int numberOfColumns = rsmd.getColumnCount();
/* 2877 */         for (int col = 1; col <= numberOfColumns; col++) {
/* 2878 */           if (rsmd.getColumnTypeName(col).indexOf("TIMESTAMP") != -1) {
/* 2879 */             this._nativeTimestampsMap.put(rsmd.getColumnName(col), "TIMESTAMP");
/*      */           }
/*      */         }
/* 2882 */         nativeTimestampsFlag = true;
/*      */       }
/*      */     } else {
/* 2885 */       nativeTimestampsFlag = true;
/*      */     }
/* 2887 */     return nativeTimestampsFlag;
/*      */   }
/*      */ 
/*      */   public HashMap getNativeTimestampsMap() {
/* 2891 */     return this._nativeTimestampsMap;
/*      */   }
/*      */ 
/*      */   protected Syntax getResourceDefinedType(String columnName)
/*      */   {
/* 2900 */     ArrayList params = new ArrayList();
/* 2901 */     AccountAttributeType[] attrs = this._resource.getAccountAttributeTypes();
/* 2902 */     for (int i = 0; i < attrs.length; i++) {
/* 2903 */       String attrName = attrs[i].getMapName();
/* 2904 */       Syntax syntax = attrs[i].getSyntax();
/* 2905 */       if (columnName.equals(attrName))
/* 2906 */         return syntax;
/*      */     }
/* 2908 */     return Syntax.STRING;
/*      */   }
/*      */ 
/*      */   public static class StrictMapResolver
/*      */     implements VariableResolver
/*      */   {
/*      */     Map _map;
/*      */ 
/*      */     StrictMapResolver(Map map)
/*      */     {
/* 2645 */       this._map = map;
/*      */     }
/*      */ 
/*      */     public Object resolveVariable(String variable) throws WavesetException
/*      */     {
/* 2650 */       Object value = this._map.get(variable);
/*      */ 
/* 2652 */       if ((value == null) && (!this._map.containsKey(variable))) {
/* 2653 */         ErrorMessage msg = new ErrorMessage(Severity.ERROR, "com.waveset.adapter.RAMessages:DBTABLE_SYNC_UNKNOWN_VARIABLE", variable);
/* 2654 */         throw new WavesetException(msg);
/*      */       }
/*      */ 
/* 2657 */       return value;
/*      */     }
/*      */   }
/*      */ 
/*      */   class UpdateRow
/*      */   {
/*      */     Map _auditMap;
/*      */     Map _userMap;
/*      */ 
/*      */     public UpdateRow()
/*      */     {
/*      */     }
/*      */ 
/*      */     public UpdateRow(Map auditMap, Map userMap)
/*      */     {
/* 2625 */       this._auditMap = auditMap;
/* 2626 */       this._userMap = userMap;
/*      */     }
/*      */     public Map getAuditMap() {
/* 2629 */       return this._auditMap; } 
/* 2630 */     public Map getUserMap() { return this._userMap; } 
/* 2631 */     public void setAuditMap(Map auditMap) { this._auditMap = auditMap; } 
/* 2632 */     public void setUserMap(Map userMap) { this._userMap = userMap;
/*      */     }
/*      */   }
/*      */ 
/*      */   class UpdateRows
/*      */   {
/*      */     List _updateRows;
/*      */ 
/*      */     public UpdateRows(List updateRows, DatabaseTableResourceAdapter.UpdateRow lastOne)
/*      */     {
/* 2603 */       this._updateRows = updateRows;
/*      */     }
/*      */     public List getUpdateRows() {
/* 2606 */       return this._updateRows; } 
/* 2607 */     public void setUpdateRows(List updateRows) { this._updateRows = updateRows;
/*      */     }
/*      */   }
/*      */ 
/*      */   public static class VariableChecker
/*      */     implements VariableResolver
/*      */   {
/*      */     private Map _schemaMap;
/*      */ 
/*      */     public VariableChecker(Map schemaMap)
/*      */     {
/* 2572 */       this._schemaMap = schemaMap;
/*      */     }
/*      */ 
/*      */     public Object resolveVariable(String variable)
/*      */       throws WavesetException
/*      */     {
/* 2585 */       if (!this._schemaMap.containsValue(variable)) {
/* 2586 */         ErrorMessage msg = new ErrorMessage(Severity.ERROR, "com.waveset.adapter.RAMessages:DBTABLE_SYNC_UNKNOWN_VARIABLE", variable);
/* 2587 */         throw new WavesetException(msg);
/*      */       }
/*      */ 
/* 2590 */       return "";
/*      */     }
/*      */   }
/*      */ 
/*      */   private static class DatabaseTableAccountIterator
/*      */     implements AccountIterator
/*      */   {
/*      */     private static final String CLASS = "com.waveset.adapter.DatabaseTableResourceAdapter$DatabaseTableAccountIterator";
/* 1633 */     private static final Trace TRACE = TraceManager.getTrace("com.waveset.adapter.DatabaseTableResourceAdapter$DatabaseTableAccountIterator");
/*      */     private DatabaseTableResourceAdapter _adapter;
/*      */     private Connection _con;
/*      */     private PreparedStatement _st;
/*      */     private ResultSet _res;
/*      */     private boolean _eof;
/*      */ 
/*      */     public DatabaseTableAccountIterator(Connection con, DatabaseTableResourceAdapter adapter, Map searchParams)
/*      */       throws WavesetException
/*      */     {
/* 1672 */       String METHOD = "DatabaseTableAccountIterator";
/* 1673 */       TRACE.entry1("DatabaseTableAccountIterator");
/* 1674 */       if (TRACE.level1("DatabaseTableAccountIterator")) {
/* 1675 */         StringBuffer sb = new StringBuffer(64);
/* 1676 */         sb.append("connection=");
/* 1677 */         sb.append(con);
/* 1678 */         sb.append("adapter=");
/* 1679 */         sb.append(adapter);
/* 1680 */         sb.append("searchParams=");
/* 1681 */         if (searchParams != null)
/* 1682 */           sb.append(searchParams.toString());
/*      */         else {
/* 1684 */           sb.append("null");
/*      */         }
/* 1686 */         TRACE.info1("DatabaseTableAccountIterator", sb.toString());
/*      */       }
/*      */ 
/* 1689 */       boolean success = false;
/*      */       try
/*      */       {
/* 1695 */         String keyColumn = (String)adapter.getRequiredResAttrVal("Key Column");
/* 1696 */         StringBuffer selectList = new StringBuffer();
/* 1697 */         adapter.addQuotedName(selectList, keyColumn);
/* 1698 */         Resource resource = adapter.getResource();
/* 1699 */         AccountAttributeType[] attrs = resource.getAccountAttributeTypes();
/* 1700 */         for (int i = 0; i < attrs.length; i++) {
/* 1701 */           String columnName = attrs[i].getMapName();
/* 1702 */           if (Util.length(columnName) <= 0)
/*      */           {
/*      */             continue;
/*      */           }
/* 1706 */           if (!Util.equalStrings(columnName, keyColumn)) {
/* 1707 */             selectList.append(", ");
/* 1708 */             adapter.addQuotedName(selectList, columnName);
/*      */           }
/*      */ 
/*      */         }
/*      */ 
/* 1716 */         String whereClause = "";
/* 1717 */         if (searchParams != null) {
/* 1718 */           String whereClauseObj = (String)searchParams.get("searchFilter");
/* 1719 */           if (Util.length(whereClauseObj) > 0) {
/* 1720 */             whereClause = " where " + whereClauseObj;
/*      */           }
/*      */ 
/*      */         }
/*      */ 
/* 1726 */         String orderByClause = "";
/* 1727 */         if (searchParams != null) {
/* 1728 */           String orderByClauseObj = (String)searchParams.get("orderBy");
/* 1729 */           if (Util.length(orderByClauseObj) > 0) {
/* 1730 */             orderByClause = " ORDER BY " + orderByClauseObj;
/*      */           }
/*      */         }
/*      */ 
/* 1734 */         selectList.insert(0, "select ");
/* 1735 */         selectList.append(" from ");
/* 1736 */         selectList.append(adapter.getRequiredResAttrVal("Table"));
/* 1737 */         selectList.append(whereClause);
/* 1738 */         selectList.append(orderByClause);
/* 1739 */         String q = selectList.toString();
/*      */ 
/* 1747 */         if (TRACE.level3("DatabaseTableAccountIterator")) {
/* 1748 */           StringBuffer msg = new StringBuffer("Executing query: ");
/* 1749 */           msg.append(q);
/* 1750 */           TRACE.info(0L, "DatabaseTableAccountIterator", msg.toString());
/*      */         }
/*      */ 
/* 1753 */         this._adapter = adapter;
/* 1754 */         this._con = con;
/* 1755 */         this._st = this._con.prepareStatement(q);
/* 1756 */         this._res = this._st.executeQuery();
/*      */ 
/* 1759 */         if ((this._res == null) || (!this._res.next())) {
/* 1760 */           this._eof = true;
/*      */         }
/*      */         else
/*      */         {
/* 1764 */           success = true;
/*      */         }
/*      */       }
/*      */       catch (Exception e) {
/* 1768 */         TRACE.throwing1("DatabaseTableAccountIterator", e);
/* 1769 */         throw new InternalError("DatabaseTableAccountIterator", e);
/*      */       }
/*      */       finally {
/* 1772 */         if (!success)
/* 1773 */           close();
/*      */       }
/* 1775 */       TRACE.exit1("DatabaseTableAccountIterator");
/*      */     }
/*      */ 
/*      */     public void close()
/*      */     {
/*      */       try
/*      */       {
/* 1784 */         if (this._res != null)
/* 1785 */           this._adapter.closeResult(this._res);
/*      */       } catch (Throwable t) {
/*      */       }
/*      */       finally {
/* 1789 */         this._res = null;
/*      */       }
/*      */       try
/*      */       {
/* 1793 */         if (this._st != null)
/* 1794 */           this._adapter.closeStatement(this._st);
/*      */       } catch (Throwable t) {
/*      */       }
/*      */       finally {
/* 1798 */         this._st = null;
/*      */       }
/*      */       try
/*      */       {
/* 1802 */         if (this._con != null)
/* 1803 */           this._adapter.closeConnection(this._con);
/*      */       } catch (Throwable t) {
/*      */       }
/*      */       finally {
/* 1807 */         this._con = null;
/*      */       }
/*      */ 
/* 1810 */       this._eof = true;
/*      */     }
/*      */ 
/*      */     public boolean hasNext()
/*      */       throws WavesetException
/*      */     {
/* 1817 */       return !this._eof;
/*      */     }
/*      */ 
/*      */     public WSUser next()
/*      */       throws NoSuchElementException
/*      */     {
/* 1826 */       String METHOD = "next";
/* 1827 */       TRACE.entry1("next");
/* 1828 */       if (this._eof) {
/* 1829 */         NoSuchElementException nsee = new NoSuchElementException();
/* 1830 */         TRACE.throwing1("next", nsee);
/* 1831 */         throw nsee;
/*      */       }
/*      */ 
/* 1834 */       WSUser user = null;
/*      */       try {
/* 1836 */         String id = this._res.getString(1);
/* 1837 */         if (id != null) {
/* 1838 */           id = id.trim();
/*      */         }
/* 1840 */         user = new WSUser();
/* 1841 */         user.setName(id);
/*      */ 
/* 1845 */         Resource resource = this._adapter.getResource();
/* 1846 */         ResourceInfo info = new ResourceInfo();
/* 1847 */         info.setResource(resource);
/* 1848 */         info.setAccountID(id);
/* 1849 */         user.addResourceInfo(info);
/*      */ 
/* 1851 */         AccountAttributeType[] attrs = resource.getAccountAttributeTypes();
/* 1852 */         for (int i = 0; i < attrs.length; i++) {
/* 1853 */           String attrMapName = attrs[i].getMapName();
/* 1854 */           String val = this._res.getString(attrMapName);
/* 1855 */           if (this._adapter.checkNativeTimestamps(resource, this._res))
/*      */           {
/* 1857 */             String mapVal = (String)this._adapter.getNativeTimestampsMap().get(attrMapName);
/* 1858 */             if ((mapVal != null) && (mapVal.equals("TIMESTAMP")))
/*      */             {
/* 1860 */               Object ts = this._res.getTimestamp(attrMapName);
/* 1861 */               val = ts != null ? ts.toString() : null;
/*      */             }
/*      */           }
/*      */ 
/* 1865 */           if (val != null) {
/* 1866 */             user.setAttribute(attrs[i].getName(), val);
/*      */           }
/*      */ 
/*      */         }
/*      */ 
/* 1871 */         if (!this._res.next()) {
/* 1872 */           this._eof = true;
/* 1873 */           close();
/*      */         }
/*      */       }
/*      */       catch (SQLException e) {
/* 1877 */         TRACE.throwing1("next", e);
/*      */ 
/* 1880 */         this._eof = true;
/* 1881 */         close();
/* 1882 */         throw new NoSuchElementException();
/*      */       }
/*      */ 
/* 1885 */       TRACE.exit(0L, "next", user);
/* 1886 */       return user;
/*      */     }
/*      */   }
/*      */ 
/*      */   public static class Param
/*      */   {
/*      */     public Syntax syntax;
/*      */     public Object value;
/*      */     public String colName;
/*      */ 
/*      */     public Param(String colName, Syntax syntax, Object value)
/*      */     {
/* 1057 */       this.colName = colName;
/* 1058 */       this.syntax = syntax;
/* 1059 */       this.value = value;
/*      */     }
/*      */   }
/*      */ }

/* Location:           E:\IDM_Code\idmadapter.jar
 * Qualified Name:     com.waveset.adapter.DatabaseTableResourceAdapter
 * JD-Core Version:    0.6.0
 */